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: cron accepts mon and jan, and both 0 and 7 mean Sunday

Cron
~/tips/tip-cron-names-and-sunday-7

30 6 * * mon /usr/local/bin/weekly-report.sh

Makes crontab lines easier to read with day and month names, and explains the two Sundays.

Numbers for weekdays are easy to get wrong. Is 1 Monday or Sunday? cron lets you write the first three letters of the name instead.

30 6 * * mon   /usr/local/bin/weekly-report.sh
0 2 1 jan *   /usr/local/bin/yearly-archive.sh

Day names (sun, mon up to sat) work in the last field and month names (jan to dec) in the fourth. Case doesn’t matter.

For numbers, the day of week runs from 0 to 7, and both 0 and 7 are Sunday. That’s on purpose, so that people who count from Sunday and people who count from Monday can both write a range: 1-7 is Monday to Sunday.

Here’s where implementations differ. cronie, used on Fedora, RHEL and Arch, allows ranges and lists of names, so mon-fri works. Debian and Ubuntu use a cron based on Vixie cron, and its man page says ranges or lists of names are not allowed. There, mon-fri is an error and you have to write 1-5. A single name like mon works everywhere.

So for a crontab that has to work on any system, I’d use numbers for ranges and names only for single days. If you’re not sure which cron you have, man 5 crontab on the machine tells you in the section about names.

Further reading: crontab(5) manual page, Debian cron, crontab(5) manual page, cronie

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 @daily /usr/local/bin/backup.sh sudo ls -l /var/spool/cron/crontabs All Cron tips (32) All topics

Link