Running a Cron Job every other week
You can't specify in the cron schedule for the day of week to be divided unlike some of the other schedules, nor can you use the # to get consistent results.
The answer is to use an epxression in cron command:
0 10 * * 1 [ `expr \`date +\%s\` / 86400 \% 2` -eq 1 ] && <my command>
This will run this command every monday and by determining whether it's an odd or even day since the unix epoch will run the command
To determine whether a command would run on a specific day use the following command:
expr `date +%s -d "today"` / 86400 \% 2
a 1 means it will run a 0 means it won't. Adjust the -eq 1
to -eq 0
to adjust the day the command runs on.
Written by Kevin Dew
Related protips
3 Responses
Hi,
how do i set this up to run every other friday?
over 1 year ago
·
the 1 in 0 10 * * 1
specifies Monday so adjust this to be 5 for Friday (0 10 * * 5
)
over 1 year ago
·
Try a cron job generator at http://www.crontab-generator.org/, very good.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Cron
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#