Systemd Timers

Systemd timers are the modern alternative to cron for scheduling tasks on Ubuntu. Unlike cron, timer output goes to the systemd journal (accessible via journalctl), timers integrate with systemd's dependency system (a timer can require a network connection before running), and missed runs can be automatically retried when the system comes back online. For any new scheduled task on Ubuntu 20.04+, prefer systemd timers over cron.

Timers vs cron

FeatureCronSystemd Timers
LoggingRedirect manually to logfileAutomatic via journald
DependenciesNoneFull systemd dependency graph
Missed runsSilently skippedCan run on next boot with Persistent=true
Output handlingLocal mail or redirectCaptured in journal automatically
Randomized delayNot supportedRandomizedDelaySec= prevents thundering herd
ComplexitySimple syntaxRequires two files (service + timer)

Creating a systemd timer

A systemd timer requires two files: a .service file that defines what to run, and a .timer file that defines when to run it.

sudo nano /etc/systemd/system/backup.service

/etc/systemd/system/backup.service

[Unit]
Description=Daily database backup
After=network.target    # Only run after network is up

[Service]
Type=oneshot             # Runs once and exits (not a daemon)
User=root
ExecStart=/usr/local/bin/backup.sh
StandardOutput=journal   # Output goes to systemd journal
StandardError=journal
sudo nano /etc/systemd/system/backup.timer

/etc/systemd/system/backup.timer

[Unit]
Description=Run daily backup at 2:00 AM

[Timer]
OnCalendar=*-*-* 02:00:00       # Every day at 2:00 AM
                                 # Format: year-month-day hour:minute:second
RandomizedDelaySec=300           # Random delay up to 5 minutes (prevents thundering herd)
Persistent=true                  # Run missed executions on next boot

[Install]
WantedBy=timers.target
# Enable and start the timer:
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer

# Verify it is active:
systemctl status backup.timer

systemctl status backup.timer output

backup.timer - Run daily backup at 2:00 AM
     Loaded: loaded (/etc/systemd/system/backup.timer; enabled)
     Active: active (waiting)
    Trigger: Mon 2025-06-10 02:00:00 UTC; 14h left
   Triggers: backup.service

Managing timers

# List all active timers and their next run times:
systemctl list-timers

systemctl list-timers output

NEXT                        LEFT      LAST                        PASSED  UNIT
Mon 2025-06-10 02:00:00 UTC 14h left  Sun 2025-06-09 02:00:05 UTC 10h ago backup.timer
Mon 2025-06-10 00:00:00 UTC 12h left  Sun 2025-06-09 00:00:01 UTC 12h ago logrotate.timer
# View logs for a specific service run by the timer:
journalctl -u backup.service         # All runs
journalctl -u backup.service -n 50   # Last 50 lines
journalctl -u backup.service --since "2 hours ago"

# Manually trigger a timer's service (test it now):
sudo systemctl start backup.service

# Disable a timer:
sudo systemctl disable --now backup.timer

# Common OnCalendar values:
# daily           → *-*-* 00:00:00
# weekly          → Mon *-*-* 00:00:00
# monthly         → *-*-01 00:00:00
# hourly          → *-*-* *:00:00
# *-*-* *:00/15   → Every 15 minutes

Conclusion

Use Persistent=true in timers for any job that should never be missed — backup jobs, security scans, certificate renewals. If the server is powered off at the scheduled time, Persistent=true causes the job to run at the next startup. Use RandomizedDelaySec= when you have multiple servers that run the same timer to prevent them all from hitting the same external resource (database, API) at exactly the same moment.

FAQ

Is Systemd Timers important for Ubuntu administrators?+

Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.

Should I practice this on a live server?+

Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.

What should I do after reading this article?+

Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.

Need help with Ubuntu administration?

Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.

Hire Me for Support