How do I set a cron job to run twice a week?
I know how to set a cron job for every week:
0 0 * * 0
It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .
We can create that cron jobs by using crontab and list them using the crontab -l command. While creating, we can specify the date and time-related fields for the week or optionally a string that is provided of @weekly in the command for creating a cron weekly job.
As we already know that the pattern * */2 * * * executes the task for every 2 hour, similary can * * */2 * * execute the task for every 2 day (once in 2 days).
1 Answer. The cron statement for each script execute on the same days - */2 evaluates to 1-31/2 . See my answer here for more details. To get alternating days, you could use 2-31/2 for the first script - this would start at 2 and skip every other for 2,4,6 etc.
How about the following:
0 0 * * 1,4
This sets the day of week to Monday (1) and Thursday (4). You can choose any values 0–7 (both 0 and 7 are Sunday).
For a more readable crontab, you can also use names:
0 0 * * MON,THU
See also: How to instruct cron to execute a job every second week?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With