On Ubuntu 16, I'm trying to trigger cron job at 8:00 AM on workdays, and here is what I've tried so far
0 8 * * 1-5 /path/to/command
0 8 * * MON-FRI /path/to/command
Does not work
These syntaxes are valid for all working days a 8:00 AM :
0 8 * * 1-5 /path/to/command >/dev/null 2>&1
0 8 * * 1,2,3,4,5 /path/to/command >/dev/null 2>&1as you said @aleksandar-pavić
More explanations with these links :
The use of
>/dev/null 2>&1 is optional, the goal is to redirect all the outputs to /dev/null.
You must have another problem, you must also specify the user if you use crontab -e
Here is a reminder of the cron syntax
* * * * * * | | | | | | | | | | | +-- Year (range: 1900-3000) | | | | +---- Day of the Week (range: 1-7, 1 standing for Monday) | | | +------ Month of the Year (range: 1-12) | | +-------- Day of the Month (range: 1-31) | +---------- Hour (range: 0-23) +------------ Minute (range: 0-59)
Finally tested and found the answer, so 1-5, or MON-FRI does not work, what works is
0 8 * * 1,2,3,4,5 /path/to/command
weekday: any day of the week except Sunday or, often, Saturday and Sunday.
workday: a day on which work is done; working day.
If you wanted workdays, you'd have to schedule a script every day with
cron, and then build into that script the logic necessary to determine whether "today" is a working day and the command needs to be executed, or not.
Since the question is tagged
ubuntu-16.04, I had a look at the relevant man page which reads:
day of week 0-7 (0 or 7 is Sun, or use names)
Names can also be used for the
day of weekfields. Use the first three letters of the particular day or month (case doesn't matter). Ranges or lists of names are not allowed.
and then tried this crontab on a Ubuntu 16.04 server:
* * * * 1-5 date >> /tmp/date.txt
It does yield the desired output:
Mon May 1 00:00:25 CEST 2017 Tue May 2 00:00:03 CEST 2017 Wed May 3 00:00:47 CEST 2017 Thu May 4 00:00:01 CEST 2017 Fri May 5 00:00:53 CEST 2017 Fri May 5 00:01:01 CEST 2017
This is obtained by setting the system time at 00:00:00 subsequently for each day of the first week of May 2017. The results also highlight that cron does not guarantee execution with a precision interval smaller than one minute (if interested, see a question on this topic).
Just out of curiosity, I the tried the following crontab:
* * * * MON-FRI date >> /tmp/date.txt
Surprisingly, and contrary to what the documentation says, it does still work:
Mon May 1 00:00:44 CEST 2017 Tue May 2 00:00:39 CEST 2017 Wed May 3 00:00:47 CEST 2017 Thu May 4 00:00:17 CEST 2017 Fri May 5 00:00:10 CEST 2017