So I need a Cron expression which will run my service in following interval:
-every 2h;
-during work hours 9-17h;
-only on work days MON-FRI;
-every month;
-of every year;
I've come up with this:
#Scheduled(* * 0/2 * * MON-FRI)
public Object updateDB() {
controllerImpl.updateDB();
return new Object();
}
I think the problem here s every 2 hours between 9-17h, isn't it?
Try this:
#Scheduled(* * 9-17/2 * * MON-FRI)
You can try following cron expression:
#Scheduled(* * 9-17/2 ? * MON-FRI)
For future references you can use following to create your cron expression:
https://www.freeformatter.com/cron-expression-generator-quartz.html
Related
i have a list of cron entries in my database which has only values for date,day of month,day of week.
Sample cron values in my database.
"0 0 * * * "
" * * 15 2 "
" * * * * MON-FRI"
How i do check if there are any cron entry in my database that is applicable for today?i have to use this in my java spring boot application.
You can use cronexpression of quartz to get for next valid fire date of cron.
Ex.
String orgTimeZone = "US/Eastern";
String cron = "0 10 2 ? * MON,WED *";
CronExpression exp = new CronExpression(cron);
exp.setTimeZone(TimeZone.getTimeZone(orgTimeZone));
Date date = new Date();
Date nextValidFireTime = exp.getNextValidTimeAfter(date);
ZonedDateTime nextExecDate = zonedDateTimeConverter.toZonedDateTime(nextValidFireTime, orgTimeZone);
ZonedDateTime currentDate = zonedDateTimeConverter.toZonedDateTime(date, orgTimeZone);
System.out.println(nextExecDate);
ZonedDateTime after1Hourdate = currentDate.plusHours(1);
System.out.println(currentDate + ":" + after1Hourdate);
if (nextExecDate.isAfter(currentDate) && nextExecDate.isBefore(after1Hourdate)) {
System.out.println("Match Cron");
}
I've got a strange issue.
I use a custom command to share, via FB api, some url.
Now this command is scheduled to a fixed cron configuration but I would like this to be randomic, maybe between time ranges.
I tried using a random sleep function inside command, but I got strange behavior.
Did anyone already have to face this problem?
One solution is to put the code you want to run into a Queued Job, and then delay that job a random amount. For example, this code will run at a random time every hour (the same can easily be extrapolated to random time every day or on some other interval):
$schedule
->call(function () {
$job = new SomeQueuedJob();
// Do it at some random time in the next hour.
$job->delay(rand(0, 60 * 59));
dispatch($job);
})
->hourly();
You can generate random time at beginning.
protected function schedule(Schedule $schedule)
{
$expression = \Cache::get('schedule:expressions:inspire');
if (!$expression) {
$randomMinute = random_int(0, 59);
// hour between 8:00 and 19:00
$hourRange = range(8, 19);
shuffle($hourRange);
// execute twice a day
$randomHours = array_slice($hourRange, 0, 2);
$expression = $randomMinute . ' ' . implode($randomHours, ',') . ' * * * *';
\Cache::put('schedule:expressions:inspire', $expression, Carbon::now()->endOfDay());
}
$schedule->command('inspire')->cron($expression);
}
If I put the following in crontab -e:
* * * * * date +"%Y-%m-%d" > /home/apps/temp/env.txt
there is no env.txt created.
If I change the above line to:
* * * * * date > /home/apps/temp/env.txt
env.txt is created properly.
How can I format date in cron?
You need to escape each one of the %:
* * * * * date +"\%Y-\%m-\%d" > /home/apps/temp/env.txt
Or even better, remove the quotes and leave like this:
* * * * * date +\%Y-\%m-\%d > /home/apps/temp/env.txt
Using Ruby
I would like that any ranges that appear in a cron to be expanded to the contained numbers in the range ex:
0,5,7,30-35 1,3-8,20 * * * /script.sh
expanded :
0,5,7,31,32,33,34,35 1,3,4,5,6,7,8,20 * * * /script.sh
Any help is appreciated
Assuming that 0,5,7,30-35 1,3-8,20 * * * /script.sh is a string, a simple gsub would work:
str = "0,5,7,30-35 1,3-8,20 * * * /script.sh"
str.gsub(/(\d+)-(\d+)/) { ($1..$2).to_a.join(',') }
#=> "0,5,7,30,31,32,33,34,35 1,3,4,5,6,7,8,20 * * * /script.sh"
Spring quartz schedule expression
0 40 4-16 * * *
Java spring scheduler quartz
I have old code with express schedule="0 40 4-16 * * *" i do not understand what does that mean ?
Even after readying the following file can't figure out what does above Cron expression do. http://en.wikipedia.org/wiki/Cron
It will run every day on 4:40, 5:40, 6:40, ... 16:40. Simple test code:
import org.springframework.scheduling.support.CronSequenceGenerator;
CronSequenceGenerator cron = new CronSequenceGenerator("0 40 4-16 * * *", TimeZone.getDefault());
Date d = new Date();
for (int i = 0; i < 20; ++i) {
d = cron.next(d);
System.out.println(d);
}
or this for Quartz API.