crontab date definition for only second Wednesday of the month

You asked for cron so only using cron you can do this using bash's date as a 2nd test:

0   0  8-14 *   *   [ "$(date '+\%u')" = "3" ] && {your script}
  • On every hour of days 8 through 14
  • and then check for wednesday with bash's date. Daynumber 3 is wednesday

The comment from Soren A is a little wrong (BUT the man page is also wrong). 0 0 8-14 * WED = “At 00:00 on every day-of-month from 8 through 14 and on Wednesday.” From the last link:

Tip 1: If the day-of-month or day-of-week part starts with a *, they form an intersection. Otherwise they form a union. * * 3 * 1 runs on the 3rd day of the month and on Monday (union), whereas * * */2 * 1 runs on every second day of the month only if it's also a Monday (intersection). The manpage is incorrect about this detail. More info.


Cron can not do this natively, but a systemd timer could.

The cron syntax 0 0 8-14 * Wed will not work. It will match every Wednesday as well as every day between the 8th and 14th. As the crontab man page says

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., don't start with *), the command will be run when either field matches the current time.

systemd timers handle the logic differently and can do what you want. This syntax would match midnight on the second Wednesday each month.

OnCalendar=Wed *-*-8..14 00:00:00

Links

  • https://www.bggofurther.com/2018/02/execute-cron-on-a-specific-day-of-the-month-e-g-second-monday/
  • https://www.redpill-linpro.com/sysadvent/2016/12/07/systemd-timers.html