SyntaxStudy
Sign Up
Linux / Bash Systemd Timers as a Modern Cron Alternative
Linux / Bash Beginner 1 min read

Systemd Timers as a Modern Cron Alternative

Systemd timers are the modern replacement for cron on systems using systemd (which includes most current Linux distributions). A timer unit schedules when a corresponding service unit is activated. This tight integration with systemd provides capabilities that cron lacks: detailed logging via the journal, dependency management, accurate missed-run handling, and the ability to run units with specific security constraints. A systemd timer consists of two files: a `.timer` unit that defines the schedule and a `.service` unit that defines the command to run. The timer can trigger on calendar events (like cron) using `OnCalendar=`, or on relative times after system boot or last run using `OnBootSec=` and `OnUnitActiveSec=`. The `Persistent=true` setting triggers a missed timer immediately after the system comes back up. Managing timers uses standard `systemctl` commands. `systemctl list-timers` shows all active timers with their next and last trigger times. This is significantly more informative than cron, which provides no built-in way to see when a job last ran or will next run. For new services on modern systems, systemd timers are generally the preferred scheduling mechanism.
Example
# ---- List active timers ----
systemctl list-timers
systemctl list-timers --all    # Include inactive timers

# ---- Create a systemd timer ----

# Step 1: Create the service unit
# sudo nano /etc/systemd/system/mybackup.service

# Contents of mybackup.service:
# [Unit]
# Description=Daily database backup
# After=network.target
#
# [Service]
# Type=oneshot
# User=backup
# ExecStart=/usr/local/bin/backup_db.sh
# StandardOutput=journal
# StandardError=journal

# Step 2: Create the timer unit
# sudo nano /etc/systemd/system/mybackup.timer

# Contents of mybackup.timer:
# [Unit]
# Description=Run daily database backup at 2 AM
# Requires=mybackup.service
#
# [Timer]
# OnCalendar=*-*-* 02:00:00
# Persistent=true
# RandomizedDelaySec=300
#
# [Install]
# WantedBy=timers.target

# Step 3: Enable and start the timer
sudo systemctl daemon-reload
sudo systemctl enable --now mybackup.timer
sudo systemctl status mybackup.timer

# ---- OnCalendar examples ----
# Daily at 2:00 AM:      *-*-* 02:00:00
# Every Monday at 6 AM:  Mon *-*-* 06:00:00
# Every 15 minutes:      *:0/15
# Hourly:                hourly  (shorthand)
# Daily:                 daily
# Weekly:                weekly
# Monthly:               monthly

# ---- Timer management ----
sudo systemctl start mybackup.timer       # Start now
sudo systemctl stop mybackup.timer        # Stop timer
sudo systemctl disable mybackup.timer     # Disable on boot

# Run the associated service immediately (test)
sudo systemctl start mybackup.service

# View logs for the service
journalctl -u mybackup.service
journalctl -u mybackup.service --since "1 hour ago"

This is the last lesson in this section.

Create a free account to earn a certificate