Skip to content

systemd timers vs. cron — what to use when

Both schedule periodic jobs; the differences in logging, environment, dependency, and observability are real. Here is the comparison and the patterns we apply.

Both cron and systemd timers schedule periodic jobs. Cron has been around since 1975 and is on every Linux box. systemd timers arrived with systemd and are the more capable option on any modern distro. The difference is not academic — logging, environment handling, dependency declaration, and what happens when the host was down at the scheduled time all behave differently. This article walks the comparison and the patterns we use for each.

How to verify

Inventory what is currently scheduled on the host.

systemctl list-timers --all
crontab -l 2>/dev/null
sudo ls -la /etc/cron.d/ /etc/cron.daily/ /etc/cron.hourly/ /etc/cron.weekly/ 2>/dev/null
sudo ls /var/spool/cron/crontabs/ 2>/dev/null
ls /etc/systemd/system/*.timer 2>/dev/null

systemctl list-timers --all is the most useful single command — it lists every timer, when it last fired, when it will fire next, and the unit it triggers. Cron is harder: jobs are scattered across /etc/cron.*, /etc/cron.d/, and per-user crontabs.

What’s happening

A cron job is a line in a crontab file. The cron daemon reads those files at start (and on changes), evaluates the schedule expressions, and execs the command at the right minute. Stdout/stderr from the command goes to email (if a mailer is configured) or /dev/null. The environment is minimal: cron sets a few variables (PATH, SHELL, HOME, USER) and that is it — your .bashrc is not sourced.

A systemd timer is a pair of unit files: a .timer that defines the schedule, and a .service that defines what to run. When the schedule fires, the timer starts the service. The service has the full power of any systemd unit: declared dependencies (After=, Wants=), environment via Environment=, resource limits via MemoryMax=/CPUQuota=, and stdout/stderr captured by journald. Failures are visible via systemctl --failed. Last-run-time is queryable.

Two things that make timers strictly better for production:

  • Persistent=true. If the host was down when the timer should have fired, systemd runs the unit as soon as the host comes back up. Cron’s anacron does something similar but only for the standard daily/weekly directories.
  • Randomized delay. RandomizedDelaySec= spreads scheduled work across a window, useful for a fleet where 50 hosts all hit a database at exactly 03:00.

Cron is still fine for: a single command on a single host, where the existing crontab is well understood. Timers are the answer for: anything that runs in production, anything that benefits from journald logging, anything that has a dependency on another service starting first.

Decision

Use cron when:

  • The host has an existing tangle of cron jobs and they are working — do not migrate for migration’s sake.
  • The job is a single line that needs to run hourly on one box and you want the simplest possible thing.
  • You are writing for a target that is not systemd-based (Alpine, some container base images, some embedded Linux).

Use systemd timers when:

  • You want the job’s logs in the journal alongside everything else.
  • The job has a dependency (must run after network, after a mount, after a database).
  • Missed-run-on-reboot semantics matter (backup jobs, security scans).
  • You want to spread the schedule across a fleet via RandomizedDelaySec=.
  • You want systemctl --failed to surface failed runs.
  • You want resource limits.

Our fleet default is timers. Cron remains on hosts where vendor packages drop into /etc/cron.d/; we leave those alone.

The procedure

  1. For a one-off cron job.

    crontab -e

    Add (this is the standard five-field schedule):

    # min  hour  day  month  weekday  command
    15     3     *    *      *        /usr/local/bin/backup-db.sh > /var/log/backup-db.log 2>&1

    Save. cron re-reads the file. Verify:

    crontab -l
    sudo journalctl -t CRON --since today

    journalctl -t CRON catches the cron daemon’s own messages (start/exec). Job stdout/stderr is not in the journal unless the command redirects there.

  2. For a systemd timer. Two files. The service:

    # /etc/systemd/system/db-backup.service
    [Unit]
    Description=Stack Harbor — daily DB backup
    After=network-online.target postgresql.service
    Wants=network-online.target
    
    [Service]
    Type=oneshot
    User=postgres
    ExecStart=/usr/local/bin/backup-db.sh
    StandardOutput=journal
    StandardError=journal

    The timer:

    # /etc/systemd/system/db-backup.timer
    [Unit]
    Description=Run db-backup daily at 03:15
    
    [Timer]
    OnCalendar=*-*-* 03:15:00
    RandomizedDelaySec=10m
    Persistent=true
    Unit=db-backup.service
    
    [Install]
    WantedBy=timers.target

    Then:

    sudo systemctl daemon-reload
    sudo systemctl enable --now db-backup.timer
    systemctl list-timers --all | grep db-backup
  3. OnCalendar syntax. systemd timer schedules are more readable than cron expressions.

    OnCalendar=hourly                       # top of every hour
    OnCalendar=daily                        # 00:00 every day
    OnCalendar=weekly                       # 00:00 every Monday
    OnCalendar=Mon..Fri 09:00               # 09:00 every weekday
    OnCalendar=*-*-* 03:00,15:00            # 03:00 and 15:00 every day
    OnCalendar=*-*-01 06:00                 # 06:00 on the 1st of every month
    OnCalendar=*:0/15                       # every 15 minutes

    systemd-analyze calendar "Mon..Fri 09:00" evaluates the expression and shows the next firing.

  4. Manual run of a timer’s unit.

    sudo systemctl start db-backup.service
    journalctl -u db-backup.service --since=now -f

    This runs the service immediately; useful for testing without waiting for the schedule.

  5. List, read, debug.

    systemctl list-timers
    systemctl status db-backup.timer
    journalctl -u db-backup.service --since "1 week ago"
    journalctl -u db-backup.service -p err

    The --since filters by time; -p err shows only error-level entries.

  6. Migrate a cron job to a timer. Read the cron line, identify command + schedule, write the two unit files. The conversion is mechanical for simple cases.

  7. For a periodic job that should run on last day of month, use ~.

    OnCalendar=monthly
    # equivalent to: *-*-01 00:00:00

    Or for “the last Sunday of every month”:

    OnCalendar=Sun *-*-25..31 02:00:00
  8. Use OnBootSec= and OnUnitActiveSec= for relative schedules. Useful when you want “5 minutes after boot, then every hour.”

    [Timer]
    OnBootSec=5min
    OnUnitActiveSec=1h

Operational notes

  • A cron job that writes nothing to stdout/stderr is invisible to journald unless it explicitly logs to syslog. With a timer, the unit’s stdout/stderr is in the journal by default.
  • Persistent=true only catches up one missed run per unit, not every missed run since the last reboot. For “run every missed slot,” you need a custom loop in the script.
  • RandomizedDelaySec= is per-timer; the actual jitter is uniformly distributed in [0, X]. For a fleet of 100 hosts and a 10-minute delay, the effective load spread is the timer’s nominal time + 0 to 10 minutes.
  • Cron’s MAILTO= sends stdout/stderr to email; set MAILTO="" to disable, otherwise a noisy job spams your mailbox.
  • Systemd timers cannot directly express “every Thursday at 14:00 except holidays.” For business-day logic, embed it in the script.
  • A timer with no [Install] section will not enable; the file is valid but systemctl enable says nothing happens. Always include WantedBy=timers.target.

In the engagements we run, every scheduled job is a systemd timer, the unit file is in the same Git repo as the rest of the host’s config, and systemctl --failed is one of the metrics we alert on across the fleet. That uniformity is what makes “what is supposed to run on this host” a five-second question instead of a fifteen-minute audit — and it is part of the Managed Operations observability floor.