How to Use Systemd Timers as a Superior Alternative to Cron Jobs in Linux

Introduction to Systemd Timers vs Cron

For decades, Cron has been the default task scheduler in Linux. However, as modern Linux distributions have transitioned to systemd, a new and vastly superior alternative has emerged: systemd timers. Systemd timers offer granular execution control, dependency management, detailed logging via journalctl, and microsecond accuracy, none of which are possible with traditional cron jobs.

Why Replace Cron with Systemd Timers?

Systemd timers solve several persistent issues with cron:

  • Missed jobs during downtime: If a machine is powered off when a cron job is scheduled, the job is missed. Systemd timers can use Persistent=true to execute missed jobs immediately upon boot.
  • Logging and Debugging: Cron logs are notoriously difficult to track. Systemd timers are fully integrated with journald, meaning you can view a task’s exact output and failure reasons using standard journalctl commands.
  • Resource Control: Systemd timers can leverage cgroups to limit CPU and memory usage of the scheduled task.
  • Dependencies: You can configure a timer to run only if the network is up, or if a specific disk is mounted.

Creating Your First Systemd Timer

To schedule a task with systemd, you need two files: a Service file (which defines what to run) and a Timer file (which defines when to run it).

Step 1: Create the Service Unit

Create a file named /etc/systemd/system/mytask.service:

[Unit]
Description=My Custom Maintenance Task
Wants=network-online.target
After=network-online.target

[Service]
Type=oneshot
ExecStart=/usr/local/bin/my_script.sh

Notice that we don’t include an [Install] section. This is because the service will be triggered by the timer, not enabled to run at boot.

Step 2: Create the Timer Unit

Create a matching file named /etc/systemd/system/mytask.timer:

[Unit]
Description=Run My Custom Maintenance Task Daily

[Timer]
OnCalendar=*-*-* 02:00:00
Persistent=true
RandomizedDelaySec=15m

[Install]
WantedBy=timers.target

The OnCalendar directive uses the format DayOfWeek Year-Month-Day Hour:Minute:Second. The RandomizedDelaySec adds a random delay to prevent multiple tasks from hammering the system at exactly 2 AM.

Step 3: Enable and Start the Timer

Reload the systemd daemon to read the new files, then enable and start the timer:

sudo systemctl daemon-reload
sudo systemctl enable mytask.timer
sudo systemctl start mytask.timer

Verifying and Troubleshooting Systemd Timers

To view all active timers and when they are next scheduled to run, use:

systemctl list-timers --all

To check the logs of your specific task, query the journal:

journalctl -u mytask.service

Conclusion

While cron remains adequate for simple, trivial scripts, systemd timers provide the robust, observable, and controllable execution framework required for modern Linux administration. By migrating your scheduled tasks, you gain significant reliability and troubleshooting capabilities.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.