I need crontab to run a script that transfers info from a table in mysql to another table at the last second the month. (The last second of the last day of the month).
What would the crontab execution be for this?
Thank you.
Not sure this can be done in a generic way, because the last day of the month varies and as far as I can see, the crontab syntax does not offer anything for that use case.
You may have to find out the correct date for yourself, and add individual jobs (with full dates and times, i.e. 12 per year) to the crontab.
But what do you need this for in the first place? It sounds a bit like a "smell" because you can't really rely on any job being finished before the end of the month if you start it in the last second. Would executing it at 0:00 every first day of the month not be much easier?
Stolen from this answer on Serverfault:
0 0 1 * * /usr/bin/foo
This will get you exactly what you are looking for (down to the minute - not the second). Just adjust the crontab time I mention to be 59 23 ( http://sudobash.net/?p=424 )
Cron:
2 0 * * * /usr/bin/last-day-of-the-month.sh
Script:
#!/usr/bin/bash
# last-day-of-the-month.sh
# By: Scott Rowley
# http://www.sudobash.net/
#########################################
TODAY=`/usr/local/bin/date +%d`
TOMORROW=`/usr/local/bin/date +%d -d "1 day"`
# See if tomorrow's day is less than today's
if [ $TOMORROW -lt $TODAY ]
then
echo "Today is the last day of the month, running code..."
/run/your/code
fi
exit
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 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.
I have to configure job which run on every weekdays (MON- FRI) 2 PM starting date 5-Jan-2019, considering today's date is 26-dec-2018
0 14 * * 1-5 (this will work for mon-fri 2 Pm) but i need to add start date as well.
You can't add start date because this is not how cron work. Moreover the relation between date and month is AND, but between date, month and day of week is OR. So the best way is to add this login in script. Can be something like:
TDATE=$(date +%Y%m%d)
if [ "$TDATE" -ge 20190105 ]
then exec your code
fi
I'm trying to come up with a CRON expression that will allow me to schedule a quartz trigger to run on every Monday in a month except the first one.
References:
http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html
https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm
CRON allows you to specify the nth occurrence of a day of the week easily. An expression for First Monday of the month would be:
0 5 0 ? * 2#1
Where the 2#1 represents the first Monday of the month (2 = day of the week, 1 being the nth occurrence)
However, if I try to do something like
0 5 0 ? * 2#2-2#5
OR
0 5 0 ? * 2#2,2#3,2#4,2#5
It complains with the message
Support for specifying multiple "nth" days is not implemented.
Does anyone know how to achieve this in CRON?
Where cron doesn't give you the expressiveness you desire(a), it's a simple matter to change the command itself to only execute under certain conditions.
For your particular case, you know that the first Monday of a month is between the first and seventh inclusive and subsequent Mondays must be on the eighth or later.
So use cron to select all Mondays but slightly modify the command to exclude the first one in the month:
# mm hh dom mon dow command
0 1 * * 1 [[ $(date +%u) -gt 7 ]] && doSomething
That job will run at 1am every Monday but the actual payload doSomething will only be executed if the day of the month is greater than seven.
Some people often opt for putting the test into the script itself (assuming it even is a script) but I'm not a big fan of that, preferring to keep all scheduling information in the crontab file itself.
(a) Don't be mistaken into thinking you can combine the day of week 1 and day of month 8-31 to do this. As per the man page, those conditions are OR'ed (meaning either will allow the job to run):
Commands are executed by cron when the minute, hour, and month of year fields match the current time, and when at least one of the two day fields (day of month, or day of week) match the current time
Combining those two will run the job on the first Monday and every single day from the eighth onwards.
I'm not very well versed in Cron expressions and struggling to find out whether something is feasible. Is it possible to create an expression for every 2nd Wednesday of the month minus 52 days?
The 2nd Wednesday of the month our known starting point and then want to subtract off it to run that on an ongoing basis. Maybe there is a smarter way to write this?
Thanks!
I don't think you can do it directly in the cron time specification. I would do it by running a command every Sunday (52 days before a Wednesday is a Sunday) and then check if the date 52 days into the future is between 8 and 13, inclusive:
0 0 * * 7 DATE=$(date --date='now+52 days' +\%-d); [ $DATE -ge 8 -a $DATE -le 13 ] && some command
(Note that percent signs are special in crontabs and need to be escaped.)