17

I added this to /etc/crontab on a few different linux & freebsd systems:

# monthly reboot: 3rd Tuesday of every month
56 07 15-21 * 2 root /sbin/shutdown -r now

I want a reboot on the 3rd TUESDAY of every month. However, all the systems rebooted on the 3rd Wednesday of this month (the 19th).

What am I doing wrong?

Update: Thanks to Ranon's answer below, looks like the below revision will work, can anyone confirm or is there an even better way of doing it?

# monthly reboot: 3rd Tuesday of every month
56 07 15-21 * * root test $(date +\%u) -eq 2 && /sbin/shutdown -r now

1 Answer 1

18

Have a look at man 5 crontab.

Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example, ``30 4 1,15 * 5'' would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result by adding a test to the command (see the last example in EXAMPLE CRON FILE below).

So your servers should restart every day from 15-21 AND each tuesday.

Have a look at the manpages' example:

# Run on every second Saturday of the month
0 4 8-14 * *    test $(date +\%u) -eq 6 && echo "2nd Saturday"
2
  • 8
    This seriously seems like a design bug. If I wanted every day from 15-21 AND each Tuesday I'd make two separate crontab entries. Having to resort to a separate test is an ugly hack. That said, thanks for providing a workaround! Oct 10, 2016 at 20:32
  • @JensErat I believe the percent sign % need to be escaped to be valid crontab format.
    – Yuki Inoue
    May 5, 2020 at 9:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .