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/warmupcrontab -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 * 5runs 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 +\%Ymust be escaped —date +%Yin a crontab silently does something else entirely.- Output goes to local mail. Mail that nobody reads.
MAILTO=""silences it,MAILTO=you@xsends it — but the honest fix is>> /var/log/job.log 2>&1on 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/jobis the standard guard: second copy exits instantly instead of doubling the pileup. Wrap intimeout 600for 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 slowdaemon-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 reclaimlogger "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
PATHandSHELL=/bin/bashat the top of crontabs; absolute paths inside jobs. flock -n+timeouton every scheduled job; log to a file or the journal, not mail.daemon-reloadafter unit edits;list-timersto verify the next fire time.journalctl -u -finstead oftail -f /var/log/...on any modern distro.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Red Hat Called This Bug a 5.9. Two Weeks Later It Was a 9.8.
CVE-2026-98432 is a use-after-free in the IPv4 TCP handshake, reachable by anybody who can reach a listening port, sitting in the kernel since 4.4. Red Hat scored it 5.9 moderate and deferred the fix. Then 8.1. Then 9.8.
Self-Host OpenClaw: VPS Setup to Production (2026)
Self-host OpenClaw from zero to production. Server provisioning, SSH lockdown, Docker, reverse proxy, Fail2ban, performance tuning, and full backup strategy in one guide.
Self-Hosting Prometheus and Grafana: Infrastructure Monitoring That Actually Works
Build a production-grade monitoring stack with Prometheus and Grafana from scratch. Covers architecture, Docker Compose deployment, Node Exporter, alerting with Alertmanager, PromQL from basics to advanced, pre-built dashboards, and everything SudoSecurity uses to monitor its own infrastructure.
Zero Trust Everything: The Complete Guide to Self-Hosting Teleport for Secure Infrastructure Access
SudoSecurity runs Teleport internally for everything — admin panel access, secure database connections, Kubernetes, and zero-trust philosophy across our entire team. This is the complete technical guide to installing, configuring, and running your own self-hosted Teleport cluster from scratch.