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: */7 in a cron minute field starts over every hour, so the gaps are uneven

Cron
~/tips/tip-cron-step-values-reset

*/15 * * * * /usr/local/bin/poll.sh # 15 divides 60, so every gap is 15 minutes

Explains why a cron step that does not divide 60 gives one short gap every hour.

A step value like */7 reads as “every seven minutes”, and that’s not what cron does with it. The step only counts inside its own field, so the count starts over at the top of every hour. I think this one catches people because the syntax looks so clear.

In the minute field */7 means 0 to 59 in steps of seven: :00, :07, :14 and so on up to :56. Then the hour ends and the next run is :00 again, only four minutes later. The crontab(5) man page has the extreme case: */35 doesn’t run every 35 minutes but twice an hour, at :00 and :35.

*/7  * * * *  /usr/local/bin/poll.sh   # :00 :07 ... :56, then :00 after 4 minutes
*/15 * * * *  /usr/local/bin/poll.sh   # 15 divides 60, so every gap is 15 minutes
0 */5 * * *   /usr/local/bin/poll.sh   # 0, 5, 10, 15, 20 o'clock, then 0 after 4 hours

The rule of thumb I use: in the minute field, pick a step that divides 60 (5, 10, 15, 20, 30). In the hour field pick one that divides 24. Anything else gives you one short gap per cycle. Steps also work on a range, so 0-30/10 runs at :00, :10, :20 and :30 only.

Days have the same problem, only slower. */2 in the day-of-month field runs on the 1st, 3rd and so on, so in a month with 31 days it runs on the 31st and again on the 1st, two days in a row. When you truly need “every N minutes since the last run”, cron is the wrong tool; a systemd timer with OnUnitActiveSec= counts that way.

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