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_TZ= runs a crontab on another time zone, if your cron is cronie

Cron
~/tips/tip-cron-cron-tz-time-zone

CRON_TZ=Europe/Amsterdam

Lets a crontab use local Dutch time even when the server clock runs on UTC.

Plenty of servers run on UTC, which is sensible until you want a job at 07:00 Dutch time. In Leiden that is 06:00 UTC in winter and 05:00 UTC in summer, and I’m not going to edit a crontab twice a year for that.

cronie, the cron used on Fedora and RHEL-style systems, reads a CRON_TZ variable from the crontab. It sets the time zone for that cron table, so you write the times in that zone and the daemon does the conversion:

CRON_TZ=Europe/Amsterdam
# weekdays at 07:00 Amsterdam time, summer and winter
0 7 * * 1-5  /usr/local/bin/morning-report.sh

Use a zone name like Europe/Amsterdam rather than a fixed offset, because the name carries the daylight saving rules with it. The times in the cron log are still written in the daemon’s own zone, so a job at 07:00 Amsterdam time can show up as 05:00 or 06:00 there. That’s not a bug.

The big caveat: Debian and Ubuntu ship a different cron, and their crontab(5) says plainly that per-user time zones are not supported. Every job runs on the daemon’s clock. Setting TZ= in the crontab there only changes the zone the command sees, not when it starts. On those systems my advice is a systemd timer, since OnCalendar= accepts a time zone at the end of the expression, or a job that runs every hour and checks the local time itself.

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

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