For decades, crontab -e has been the default answer whenever a Linux server needs to run a script on a schedule. Cron is simple, universal, and still works fine for quick one-liners. On modern distributions running systemd, though, .timer units solve almost every annoying edge case that cron leaves to you.
Where cron still falls short
If you have maintained cron jobs for a few years, you have probably hit at least two of these:
- Empty environment: Cron runs with a minimal
PATH(/usr/bin:/bin) and none of your shell profile variables, so scripts that work interactively fail silently at 03:00. - Overlapping runs: If a backup takes 65 minutes and runs hourly, cron cheerfully starts a second copy unless you wrap it in
flock. - No built-in missed-run catch-up: If the machine was rebooted during the scheduled minute, the job simply does not run until the next cycle.
- Clumsy logging: Cron tries to email output via a local MTA that usually isn't configured, or forces you to redirect stdout/stderr to custom log files and set up
logrotateseparately.
A minimal systemd timer pair
A systemd timer requires two small files in /etc/systemd/system/: a .service unit describing what to run, and a .timer unit describing when to run it.
# /etc/systemd/system/db-cleanup.service
[Unit]
Description=Prune expired session rows
[Service]
Type=oneshot
User=appuser
WorkingDirectory=/opt/myapp
EnvironmentFile=/opt/myapp/.env
ExecStart=/opt/myapp/bin/cleanup --days 30
Nice=10
MemoryMax=256M
# /etc/systemd/system/db-cleanup.timer
[Unit]
Description=Run daily session cleanup
[Timer]
OnCalendar=03:15:00
RandomizedDelaySec=10m
Persistent=true
[Install]
WantedBy=timers.target
Enable and start the timer (not the service):
sudo systemctl daemon-reload
sudo systemctl enable --now db-cleanup.timer
Note that RandomizedDelaySec=10m spreads out execution across a 10-minute window, which is handy if you run several VMs that all hit the same database or object storage endpoint at midnight.
Why this is nicer in practice
First, you can test the exact job environment at any time with sudo systemctl start db-cleanup.service—no editing the schedule to "two minutes from now" just to see if it works. Second, systemd will never start a second overlapping instance while a oneshot service is still running.
Third, Persistent=true records the last trigger timestamp on disk; if the server was powered off at 03:15, systemd runs the job shortly after boot. Fourth, every line of stdout and stderr goes straight into the journal, where you can inspect it with:
journalctl -u db-cleanup.service -n 50
You can also inspect every active timer, when it last ran, and when it will fire next in one table:
systemctl list-timers --all
And if you ever need to double-check systemd's calendar syntax before saving a timer, systemd-analyze calendar parses expressions and prints the next few firing times:
systemd-analyze calendar "Mon..Fri *-*-* 02:30:00" --iterations=3
You also get clean resource controls right inside the service unit: Nice=10 and IOSchedulingClass=idle keep heavy maintenance jobs from starving your web server, and MemoryMax=256M prevents a runaway script from taking down the host.
Monotonic timers for relative intervals
You do not always want wall-clock schedules. For tasks that should run 15 minutes after boot and every 6 hours after the previous run finishes, use monotonic directives like OnBootSec=15m and OnUnitActiveSec=6h instead of OnCalendar. That guarantees a slow run never piles up against the next scheduled tick.
When I still use cron
I still reach for /etc/cron.d/ when a package or an unprivileged user needs a trivial one-line script that does not warrant two unit files, or when portability to minimal non-systemd containers or BSD hosts matters. For anything running on a Linux VPS that touches backups, databases, or TLS renewals, systemd timers are worth the extra ten lines of config and make debugging failures months later far easier.