Cronjobs for alternating weekdays

November 25, 2016

I had to create a cronjob for every second and fourth sunday the other day. Turns out, this is not an easy task. Although cron is really powerful.

This is the solution:

0 4 8-14,22-28 * *    /bin/bash -c 'test $(date +\%u) -eq 7 && yourcommand.sh'

How does this work? It triggers every day between 8. and 14. as well as from 22. to 28.

The test command checks, if the day of the week is sunday / 7 (see linux day formatting).

You also have to escape the % sign in the date command.

Special thanks go out to this and this stackoverflow answers.

comments powered by Disqus