Linux β€’ Timers & Automation

How to Create a Systemd Timer to Replace Cron Jobs

Systemd timers supersede legacy crontab. They provide unified logging in journalctl, can trigger on relative intervals (`OnBootSec`, `OnUnitActiveSec`) or calendar events (`OnCalendar`), and support randomized delays to prevent thundering herd problems.

/etc/systemd/system/backup-task.service
[Unit]
Description=Database Backup Script
After=network.target

[Service]
Type=oneshot
User=root
WorkingDirectory=/opt/scripts
ExecStart=/opt/scripts/backup.sh
StandardOutput=journal
StandardError=journal
Customize:
User:
WorkingDir:
ExecStart:

Systemctl Lifecycle & Journalctl Monitoring

sudo systemctl daemon-reloadLoad both .service and .timer units
sudo systemctl enable --now backup-task.timerEnable and activate the timer schedule
sudo systemctl list-timersList all active timers with next run time and remaining countdown
sudo systemctl start backup-task.serviceManually trigger a test execution without waiting for timer

Linux & Systemd Production Best Practices

  • Enable the `.timer` unit, NOT the `.service` unit! The timer automatically activates the service when triggered.
  • `Persistent=true` ensures that if the server was powered off when the timer was scheduled to fire, it will execute immediately on the next boot.

Frequently Asked Questions About Systemd Timer (Cron Alternative)

Frequently Asked Questions

Frequently Asked Questions

Everything you need to know regarding specifications, syntax, and security best practices.

Timers log every execution cleanly to journalctl, handle missed runs during server downtime (`Persistent=true`), and support randomized delays to avoid server load spikes.