Skip to content

Lesson 10 of 14 · 22m

cron, systemd, and the Scheduling Layer

Cron runs jobs with a four-variable environment in dash; systemd timers log to the journal and catch missed runs — and `daemon-reload` is the step everyone forgets.

Your cron job runs with about four environment variables: SHELL=/bin/sh, PATH=/usr/bin:/bin, HOME, LOGNAME. No aliases, no functions, no source ~/.bashrc, no venv — and on Debian and Ubuntu, sh is dash, so set -o pipefail prints Illegal option -o pipefail and the script dies on line 2. Cron isn't broken. Your assumption that "a shell" means "my shell" is.

crontab syntax and the gotchas

# m  h  dom mon dow
  */15 *  *   *   *    /usr/local/bin/check
  0     3  *   *   *    /usr/local/bin/backup
  @daily                     /usr/local/bin/tidy
  @reboot                    /usr/local/bin/warmup

crontab -e edits your personal crontab (/var/spool/cron/crontabs/); /etc/crontab is the system file with an extra user field; /etc/cron.d and cron.{hourly,daily,weekly} are the drop-in directories.

The traps, in order of hours lost:

  • dom/dow is OR, not AND. 0 0 13 * 5 runs on the 13th and every Friday — Friday the 13th logic is the famous scheduling surprise.
  • % means newline. In the command field, % splits the command and feeds the rest to its stdin. date +\%Y must be escaped — date +%Y in a crontab silently does something else entirely.
  • Output goes to local mail. Mail that nobody reads. MAILTO="" silences it, MAILTO=you@x sends it — but the honest fix is >> /var/log/job.log 2>&1 on the job itself.
  • Jobs don't queue. A job scheduled every 5 minutes that occasionally takes 20 doesn't defer — it starts again, overlapping copies pile up, and they fight over the same lock file or table. flock -n /tmp/myjob.lock /path/job is the standard guard: second copy exits instantly instead of doubling the pileup. Wrap in timeout 600 for good measure.
  • PATH is minimal. Absolute paths inside the script, or set PATH= at the top of the crontab. The "works in my terminal, dies in cron" class of bug is this, every time.

systemd: services, not just timers

Units live in /etc/systemd/system/ (yours) and /usr/lib/systemd/system/ or /lib/systemd/system/ (the distro's — never edit those; systemctl edit nginx writes an override.conf drop-in that survives upgrades).

The commands that run daily ops:

systemctl status nginx          # state, pid, recent log lines
systemctl restart nginx         # start / stop / reload
systemctl enable --now myjob.timer
systemctl list-units --failed
systemctl daemon-reload         # after editing ANY unit file — the step everyone forgets
systemd-analyze blame           # what's making boot slow

daemon-reload deserves emphasis: systemd caches unit definitions. Edit the file, restart the service, nothing changes — because systemd never re-read it. systemctl cat nginx shows the effective unit including drop-ins, which is how you confirm what systemd thinks the service is.

journalctl: the log that replaced /var/log

The journal is structured and indexed — not a text file:

journalctl -u nginx -f                 # follow a unit
journalctl -u nginx --since "1 hour ago"
journalctl -e                          # newest
journalctl -p err -b                   # errors this boot
journalctl _COMM=cron                  # everything a program logged
journalctl --disk-usage                # and --vacuum-size=500M to reclaim

logger "backup finished" writes to it from any script.

Timers replace cron

A .timer unit paired with a .service does what cron does, better: OnCalendar=daily, OnBootSec=10m, Persistent=true (run jobs missed while the machine was off — cron never caught up; anacron filled the gap before systemd absorbed it), RandomizedDelaySec=300 to stop a fleet hitting the same API at the same second, and output lands in the journal instead of dead mail. systemctl list-timers shows NEXT and LAST — verifiable scheduling, which cron never gave you. at now + 1 hour still covers one-shots via atd.

Practice

  • Explicit PATH and SHELL=/bin/bash at the top of crontabs; absolute paths inside jobs.
  • flock -n + timeout on every scheduled job; log to a file or the journal, not mail.
  • daemon-reload after unit edits; list-timers to verify the next fire time.
  • journalctl -u -f instead of tail -f /var/log/... on any modern distro.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.