Search code examples
unixcroncron-taskcronexpression

When does this cron job get executed?


I see this cron setting in a crontab and I am curious as to when the script actually gets executed.

8 10 * * 6 expr `date +\%W` \% 2 == 1 >/dev/null || /path/to/script/scriptToRun.sh

Solution

  • Such a picky syntax...

    8 10 * * 6 expr `date +\%W` \% 2 == 1 >/dev/null || /path/to/script/scriptToRun.sh
    

    First, the cronjob:

     +---------------- minute (0 - 59)
     |  +------------- hour (0 - 23)
     |  |  +---------- day of month (1 - 31)
     |  |  |  +------- month (1 - 12)
     |  |  |  |  +---- day of week (0 - 6) (Sunday=0 or 7)
     |  |  |  |  |
     *  *  *  *  *  command to be executed
     8 10  *  *  6
    

    So in this case it means that the cronjob gets executed every Saturday at 10.08.

    Then, man date says:

    %W

    week number of year, with Monday as first day of week (00..53)

    $(date +\%W) \% 2 == 1 >/dev/null means: if the week number is not multiple of 2, then send the output to dev/null. Otherwise, proceed normally.

    So the script gets executed every other Saturday at 10.08.