Getting Started with systemd Timers: A Modern Alternative to Cron Jobs

What Are systemd Timers?

systemd timer is a modern and powerful replacement for traditional cron jobs, designed to schedule and trigger systemd service units. It simplifies automation on Linux by providing enhanced flexibility, logging, and dependency handling compared to the classic cron system.

Why Use systemd Timers Instead of Cron?

systemd timers offer several advantages over cron, making them the preferred choice for task automation on Linux environments:

  • Unified Management: Manage timers alongside other systemd units (start, stop, enable, disable).
  • Improved Logging: Use journalctl for detailed logging and status monitoring.
  • Flexible Scheduling: Supports both calendar-based syntax and monotonic (relative) timers.
  • Missed Event Handling: Automatically catch up on missed events after a reboot.

Quick Example: Run a Script Every Day at Midnight

Here’s a step-by-step guide to creating a systemd timer that runs a script daily at midnight:

1. Create a Service Unit

The service unit defines the action to be performed—in this case, running a script.

Create the file /etc/systemd/system/daily-script.service:

[Unit]
Description=Run my daily script

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

2. Create a Timer Unit

The timer unit schedules when the service should run.

Create the file /etc/systemd/system/daily-script.timer:

[Unit]
Description=Run my daily script every day at midnight

[Timer]
OnCalendar=*-*-* 00:00:00
# The format is: YYYY-MM-DD HH:MM:SS
# The above line schedules the task to run once per day at midnight.

Persistent=true

[Install]
WantedBy=timers.target

3. Enable and Start the Timer

Run the following commands to reload systemd, enable the timer, and start it immediately:

sudo systemctl daemon-reload
sudo systemctl enable --now daily-script.timer

How to Check Status and Logs

Once your timer is set up, you can monitor its status and logs using the following commands:

  • Check Active Timers:
systemctl list-timers
  • View Timer Details:
systemctl status daily-script.timer
  • Check Logs for the Script Execution:
journalctl -u daily-script.service

Reference

  1. Official systemd Documentation:
    https://www.freedesktop.org/software/systemd/man/systemd.timer.html
    This is the official documentation for systemd timers, detailing syntax, options, and examples.
  2. Arch Wiki on systemd Timers:
    https://wiki.archlinux.org/title/Systemd/Timers
    A community-driven guide with practical examples and troubleshooting tips.
  3. Red Hat Documentation on systemd Timers:
    https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/chap-managing_services_with_systemd#sect-Managing_Timers
    A detailed explanation of systemd timers for enterprise use.
  4. Ubuntu Community Help Wiki:
    https://help.ubuntu.com/community/systemd
    This page offers an overview of systemd and includes information about timers.

Summary

systemd timers are a robust and modern solution for task automation on Linux. They provide better reliability, control, and integration with systemd compared to cron jobs. By creating a simple service unit and timer unit, you can automate tasks such as backups, synchronization, cleanup jobs, and more. Whether you’re managing a personal server or enterprise infrastructure, systemd timers are an essential tool for efficient Linux automation.

Leave a Comment