This is more difficult than I thought. In the month of September 2022, I need cronjobs to run on the 1st, 3rd and 5th Thursdays (there are 5 Thursdays this month!), but in October 2022 I need them to run on the 2nd and 4th Thursdays.
I would like my tasks to run every 2 weeks without skipping any days when a month has 5 Thursdays
How can I accomplish something like this with a cronjob? Is it even possible?
You can do cron on odd/even week of month.
#Every day except Thursday at 1am
0 1 * * 0,1,2,3,5,6 yourCommand
#Every Thursdays at 1am, proceeds only on even weeks
0 1 * * 4 test $((10#$(date +\%W)\%2)) -eq 0 && yourCommand
#Every Thursdays at 6am, proceeds only on odd weeks
0 6 * * 4 test $((10#$(date +\%W)\%2)) -eq 1 && yourCommand
If you want more info regarding this please go this reference link :
cron_info
Related
I have expression 0 0 6/2 * *
it will run anything every second day starting from 6 day of month and in next month it can be earlier than 6 day? Example cron runs:
2022-06-06
2022-06-08
2022-06-10
...
2022-06-26
2022-06-28
2022-06-30
2022-07-02
2022-07-04
2022-07-06
2022-07-08
...
Does it work like this or cron run only from 6 day every month?
From here
At 12:00 AM, every 2 days, starting on day 6 of the month
So no, it will start on 6th day of month and then every 2 days
We have jobs that are scheduled to run 1 time per day - every day
We do maintenance every 3rd Sunday of the month.
Up until now every month we have manually adjusted the cron to make the job run a little later in the morning then after maintenance we reset to the desired schedule
I am trying to change cron so that we
run at 7:00am every day EXCEPT the third Sunday of the month
run at 9:00am only on the third Sunday of the month
the second item I am able to handle
0 13 15-21 * 0
however, the first has me stumped. I thought this would do the job but it will only execute this if the day is between 1-14 or 22-31 but what if the 15th is not Sunday - then it won't run.
0 11 1-14,22-31 * *
How do I tell cron to run a schedule EXCEPT the third Sunday of the month?
There is a large base of guidance on how to limit when a cron runs to a specific window but I haven't found much for how to EXCLUDE a cron from a specific window
******** UPDATE ********
I think I may have come up with an answer - not sure if it is the most efficient but
0 11 1-14,22-31 * 0
0 13 15-21 * 0
0 11 1-14,22-31 * 1-6
The above will
run at 11:00 UTC on Sunday if date is between 1-14 or 22-31
run at 13:00 UTC on Sunday if date is between 15-21 (3rd Sunday)
run at 11:00 UTC Monday through Saturday all month
If a cron job has different timing than others, then it best to just define it by itself rather than trying to combine, unless you put some code in your script to do what you actually want. Doing something in cron on some nth day of the month is a pretty well known problem. Most crontab man pages have this note:
Note: The day of a command's execution can be specified in the following two fields — 'day of month', and 'day of week'. If both fields are restricted (i.e., do not contain the "*" character), the command will be run when either field matches the crent time. For example,
"30 4 1,15 * 5" would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
So it does OR between the day of the week and the day of the month, not an AND. I don't who ever thought this was helpful, but that's the way it is. You can see solutions at:
Run every 2nd and 4th Saturday of the month
you need something like (this assumes cron runs /bin/sh):
[ `date +\%w` -eq 6 ] && <command>
on your cron job line, the above is would restrict to running only on Saturday.
I want to create a cron job in Hybris that can run 5 days before the month end.I'm do some using
0 23 22-31 * * [ $(date -d +1day +%d) -eq 1 ]
What is equivalent to this in hybris cron job impex configuration.
hybris uses Quartz 2 as shown in the documentation.
From Quartz 2 documentation :
The ‘L’ character is allowed for the day-of-month and day-of-week fields. This character is short-hand for “last”, but it has different meaning in each of the two fields. For example, the value “L” in the day-of-month field means “the last day of the month” - day 31 for January, day 28 for February on non-leap years.[...] You can also specify an offset from the last day of the month, such as “L-3” which would mean the third-to-last day of the calendar month. When using the ‘L’ option, it is important not to specify lists, or ranges of values, as you’ll get confusing/unexpected results.
So you can use that 0 0 0 L-5 * ?.
Note: on older hybris version (v4) I'm not sure Quartz 2 was available. With Quartz 1 you can't use the L-x pattern.
If you want exactly 5 days before end of month you should create 3 triggers.
0 0 20 26 1,3,5,7,8,10,12 ? * -> the 26th at 20h for all 31 days month
0 0 20 25 4,6,9,11 ? * -> the 25th at 20h for all 30 days month
0 0 20 23 2 ? * -> the 23rd at 20h for february, this is actually a corner case because you may have different day for february...
There is also an other solution, but much more complex.
You can set a trigger for the first time it needs to run. Then in your job you can access a LocaleDate object to determine the next time the job should trigger. Finally update the trigger of the cronjob with Java code or impex creation+import.
I found better solution , using quartz
0 0 0 L-5 * ? *
ref
I need a cron expression that will fire every second day excluding weekends.
Example:
The schedule starts on Monday. The schedule continues in the following manner:
(1st week) Monday>Wednesday>Friday
(2nd week) Tuesday>Thursday
(3rd week) Monday>Wednesday>Friday
(4th week) Tuesday>Thursday
Is that possible using only cron? I know a solution would be to run it every day and when it runs on weekend 'manually' prevent it from running.
Maybe something like could help...
* * 1-31/2 * mon-fri command.sh
That means, "At every minute on every 2nd day-of-month from 1 through 31 and on every day-of-week from Monday through Friday."
https://crontab.guru/#__1-31/2_*_mon-fri
http://corntab.com/?c=__1-31/2_*_MON-FRI
(Didn't tried on real machine)
I will consider extended expression format so your query will looks like:
S M H DoM M DoW Y
0 0 10 1-31 * 1#1,3#1,5#1 *
This query can be understood as: Repeat at 10:00:00 every day of every month where day of week is (monday, wednesday, friday) and it's first week of month.
You would define such 4 queries (i'm considering that 1 in 1#3 is just monday and 3 is week number in month):
1.) 0 0 10 1-31 * 1#1,3#1,5#1 *
2.) 0 0 10 1-31 * 2#2,4#2 *
3.) 0 0 10 1-31 * 1#3,3#3,5#3 *
4.) 0 0 10 1-31 * 2#4,4#4 *
which runs the same command. But it won't work becouse of limitations of most of evaluators (as i guess).
If you are familiar with .NET, I made evaluator which handle such expressions correctly, but it's only evaluator so what you only receive are dates when your event should occur. There is no job sheduler integrated with it. Click
i'm trying to do some stuff automatically every 8 Weeks, so i had open a new user crontab like this one:
crontab -e
0 9 * */2 1-5 do_this_stuff
# do it every 2 month on monday till friday at 9:00 am
This should do the job every 2 month on monday till friday on 9:00 am, but i does not. It is doing the job evey week once. Don't get it. What i'm doing wrong?
Running System is a latest debian.
regarding http://wiki.ubuntuusers.de/Cron it should run fine
The Anwer is, cron can't do a job randomly on a random day in a month. I had to change my crontab to: 0 9 1 */2 * do_sm_stuff -- this runs every two Month always on the first Day in a Month
thank you Igor