brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: a cron line with both day-of-month and weekday runs when either one matches

Cron
~/tips/tip-cron-day-of-month-and-weekday

0 9 13 * 5 /usr/local/bin/report.sh

Shows that restricting both day fields in a cron schedule widens it instead of narrowing it.

“Every Friday the 13th” looks easy to write in a crontab, and it does not do what it says.

# runs on the 13th of every month AND on every Friday
0 9 13 * 5 /usr/local/bin/report.sh

When both the day-of-month and the day-of-week field are restricted, cron fires whenever either field matches. That makes the line above run far more often than the name suggests. To get the real intersection, keep one field as * and test the other inside the command: 0 9 13 * * [ "$(date +\%u)" = 5 ] && /usr/local/bin/report.sh. Note the escaped percent sign. The either-field rule is documented in crontab(5) and holds for Vixie-derived cron implementations.

Further reading: crontab(5) manual page

Going deeper: Guide: find out why a Linux service failed, with journalctl

More Cron tips echo 'systemctl restart myapp' | at 03:00 tomorrow 55 23 28-31 * * [ "$(date -d tomorrow +\%d)" = 01 ] && /usr/local/bin/month-end.sh EDITOR=nano crontab -e 30 6 * * mon /usr/local/bin/weekly-report.sh @daily /usr/local/bin/backup.sh All Cron tips (32) All topics

Link