Sub topic 5
Choosing Between Cron and systemd Timers
Assume you are comfortable with the Linux command line, shell scripts, users, permissions, processes, and basic logs. This chapter compares two schedulers: cron and systemd timers. A scheduler starts a command at a planned time. The scheduler does not make the command reliable by itself, so you must still handle errors, logging, overlap, and permissions.
The working model
Cron reads a crontab, which is a file containing time expressions and commands. When an expression matches the current time, cron starts the command. Cron is simple and available on many Unix-like systems.
A systemd timer is a unit that activates another systemd unit, usually a service. The timer describes when to activate the service. The service describes how the job runs, including its user, timeout, dependencies, and logging.
Compare the important differences
| Concern | Cron | systemd timer |
|---|---|---|
| Configuration | One crontab entry | Separate timer and service units |
| Environment | Minimal and easy to overlook | Controlled by service settings |
| Logs | Mail or redirected output | journalctl and the systemd journal |
| Missed run after shutdown | Usually skipped | Persistent timers can catch up |
| Timeout and user | Usually handled inside the script | Unit settings provide direct controls |
| Complexity | Low for simple jobs | Higher, but more operational control |
Choose cron when the task is small, its schedule is straightforward, and compatibility matters. A short cleanup script or a user-specific report often fits well in cron.
Choose a systemd timer when the task needs a dedicated user, a timeout, journal logs, dependency ordering, missed-run behavior, or clear service status. Systemd also gives operators familiar commands such as systemctl status and journalctl.
A cron example
This entry runs a synchronization script every 15 minutes. It sets PATH because cron starts commands with a limited environment. It uses flock, a file-locking utility, so a new run exits if the previous run still holds the lock.
[email protected]
PATH=/usr/local/bin:/usr/bin:/bin
*/15 * * * * /usr/bin/flock -n /run/lock/sync.lock /usr/local/bin/sync.sh >> /var/log/sync.log 2>&1The five time fields mean minute, hour, day of month, month, and day of week. */15 in the minute field means every 15 minutes. The final redirection sends both normal output and errors to the log file.
A systemd timer example
A systemd timer normally has a matching service:
# /etc/systemd/system/backup.service
[Unit]
Description=Nightly backup
[Service]
Type=oneshot
User=backup
ExecStart=/usr/local/bin/backup.sh
TimeoutStartSec=1h# /etc/systemd/system/backup.timer
[Unit]
Description=Run the nightly backup
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
RandomizedDelaySec=5min
[Install]
WantedBy=timers.targetPersistent=true activates the service after startup if a scheduled activation was missed while the system was off. RandomizedDelaySec spreads load by adding a delay of up to five minutes. The service runs as the backup user instead of root.
Use these commands to validate and operate the timer:
systemd-analyze calendar "*-*-* 02:30:00"
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers backup.timer
sudo systemctl start backup.service
journalctl -u backup.service --since todayThe manual start tests the service without waiting for the schedule. This separation makes troubleshooting easier: first verify the service, then verify the timer.
Exercise
Exercise
Which scheduler is the better default for a backup that needs a dedicated user, a one-hour timeout, journal logs, and a catch-up run after shutdown?
Show answer
Use a systemd timer with a matching service. The service defines the user and timeout, the journal stores its output, and Persistent=true enables catch-up behavior.