I have cronjob to be run on every month first friday evening
i used the below mentioned entry
00 20 1-7 * Fri [ "$(date '+\%a')" = "Fri" ] && $HOME/path/to/my/script.sh > /dev/null 2>&1
This entry should run my script if Friday falls withing 1-7 day of the month, but my script is getting executed even after 7th (i.e on all Fridays of the month).
Please suggest how to fix it.
This is because when you specify a day of month and day of week, cron will execute the job when EITHER of those constraints are true. From the man page for crontab (5):
Note: The day of a command's execution can be specified by two fields —
day of month, and day of week. If both fields are restricted (i.e.,
aren't *), the command will be run when either field matches the cur‐
rent 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.
There isn't a direct way in cron to do what you want, but cron : how to schedule to run first Sunday of every month describes a workaround by using cron to run your script e.g. every Friday and then calculating in the script if the day of month is in the range 1-7, and only continuing when that is the case.
In response to the comment about using 5 rather than Fri to specify day of week: using Fri is OK, as the man page says:
Months or days of the week can be specified by name.
Related
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 a script to be executed each first Saturday of a quarter.
Therefore I have set a crontab line up with the following
24 9 1-7 1,4,7,10 6 /absolute/path/to/script
This script was now executed yesterday, at 9:24 (ok),on Saturay (ok), but on October(ok) 16th(NOK).
Any hints what I missed or misunderstood?
Thanks a lot.
The script runs every day the first 7 days and every Saturday of the specified months.
The reason is well explained in this crontab guru page (and crontab(5)'s man page). The relevant piece is:
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 current 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.
You can check that this is indeed what happens by checking the description and when your script will be run next ("next at") here.
The way to achieve what you want, i.e., run the script on the first Saturday, is described in other questions/answers. See, for instance, Run a cron job on the first Monday of every month? or How to schedule to run first Sunday of every month. In short, replace 6 with * and combine your command with a call to date.
I wish to schedule a airflow job for specific set of dates every month, for example 11th and last day of every month and used the below scheduler expression
25 14 11,L * * # At 2:45 pm on 11th and last day of every month
When I validated the above in https://crontab.guru/ and http://cron.schlitt.info/ i was told the expression as invalid.
Is it possible to schedule together for a known and a unknown (here last) day of every month? If not is there any other way to achieve this?
maybe your cron does not support the "L" flag. you can refer to this CRON job to run on the last day of the month
I have to schedule a job to run every 9th of the month. and if 9th falls in weekend then in should run on next monday
for example
feb 9th is sunday so it should run on feb 10th i.e. next working day.
This kind of logic won't be possible entirely from your crontab schedule. I'll also point out the possibility that Monday might not necessarily be the "next working day" (think of holidays for example) in which case you might want to account for that as well.
If you will always choose Monday when the 9th is on a weekend (regardless of that Monday being a real working day, you can schedule your cron job to run on the 9th, 10th, and 11th of every month. For example, this would run on the 9th/10th/11th at 8am every month:
* 8 9-11 * 0 your_script
After that, the logic to determine if tasks need to run will have to be handled within the script itself. If you're using Python for example:
import datetime
today = datetime.date.today()
weekday = today.weekday()
if weekday is 5 or weekday is 6:
print("it's the weekend, do nothing")
elif today.day is 9 or (today.day is not 9 and weekday is 0):
print("run the script on the 9th or the first Monday after the 9th")
else:
print("this should not happen")
(Note that the script would run on every single Monday with this logic, but if you set up the cron job only to run on the 9th/10th/11th of the month then it will work just fine)
I really hope this helps!
I need to schedule a cron job to run at 3:00 PM on the first Thursday of every month. How can I do this?
I have read another topic similar to this, but it is for the first Sunday of every month. How do I modify this to suit my needs?
Every first Sunday of very month
00 09 * * 7 [ $(date +\%d) -le 07 ] && /run/your/script
Thanks in advance!
from man 1p crontab
INPUT FILES
In the POSIX locale, the user or application shall ensure that a crontab entry is a text file consisting
of lines of six fields each. The fields shall be separated by "blank" characters. The first five fields
shall be integer patterns that specify the following:
1. Minute [0,59]
2. Hour [0,23]
3. Day of the month [1,31]
4. Month of the year [1,12]
5. Day of the week ([0,6] with 0=Sunday)
I hope that helps.