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: @daily and @weekly in a crontab replace the five time fields

Cron
~/tips/tip-cron-at-daily-shortcuts

@daily /usr/local/bin/backup.sh

Uses readable shortcuts like @daily instead of five numbers for common cron schedules.

Most cron jobs run once an hour, once a day or once a week. For those, cron has nicknames that are easier to read than five fields.

@daily   /usr/local/bin/backup.sh
@weekly  /usr/local/bin/cleanup.sh
@hourly  /usr/local/bin/check-disk.sh

The nickname replaces all five time fields, and the command follows as usual. Both Debian’s cron and cronie on Red Hat style systems support them, with these meanings:

@hourly    0 * * * *
@daily     0 0 * * *     (also @midnight)
@weekly    0 0 * * 0
@monthly   0 0 1 * *
@yearly    0 0 1 1 *     (also @annually)

So @daily means midnight and @weekly midnight between Saturday and Sunday. I like them for jobs where the exact minute doesn’t matter, because the line explains itself.

That fixed time is also the downside. Every @daily job on the machine starts at 00:00 sharp, all at once, and on a set of servers they all hit the backup target at the same second. When that matters, write the five fields and pick an odd time, like 17 3 * * *.

You can’t add anything to a nickname, so “@daily at 03:00” doesn’t exist. And these are an extension, not POSIX, so a very minimal cron, like the one in some embedded systems, may not know them.

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 30 6 * * mon /usr/local/bin/weekly-report.sh sudo ls -l /var/spool/cron/crontabs All Cron tips (32) All topics

Link