Skip to content

Timer Units

Systemd timers provide a powerful mechanism for scheduling services to run at specific times or intervals, abstracting the complexity of traditional cron jobs. A timer unit is a special type of systemd unit that manages the execution of associated service units based on predefined schedules. Timers are defined in .timer files, which are linked to corresponding .service files. When a timer triggers, it activates the service unit, allowing for precise control over when tasks are executed.


Key Components of a Timer Unit

A timer unit file (e.g., example.timer) contains a [Unit] section for metadata and a [Timer] section for scheduling logic. The core directive is OnCalendar, which specifies the schedule using a cron-like syntax. Additional directives like OnUnitActiveSec (delay after the unit becomes active) and OnUnitInactiveSec (delay after the unit becomes inactive) can refine timing behavior.

Example example.timer file:

[Unit]
Description=Run daily backup

[Timer]
OnCalendar=daily
OnUnitActiveSec=15
Unit=example.service

  • OnCalendar=daily schedules the service to run once per day.
  • OnUnitActiveSec=15 adds a 15-second delay after the service becomes active.
  • Unit=example.service links the timer to the service unit.

Relationship to Service Units

A timer unit acts as a scheduler for its associated service unit. When the timer triggers, it activates the service unit (e.g., starts, restarts, or reloads it). The service unit must be defined in a .service file, and the timer unit must explicitly reference it via the Unit= directive.

Dependencies: - The timer unit depends on the service unit being active. If the service unit is not properly configured, the timer may fail to trigger. - Timers can be configured to run one-time tasks or recurring schedules, depending on OnCalendar settings.


Example Use Cases

1. Daily Backup Task

A timer that runs a backup service every day at 2:00 AM:

[Timer]
OnCalendar=0 2 * * *
Unit=backup.service

2. Weekly System Update

A timer that triggers a system update every Sunday at 10:00 PM:

[Timer]
OnCalendar=0 22 * * Sun
Unit=update.service

3. One-Time Task

A timer that runs a script once at a specific time:

[Timer]
OnCalendar=2024-05-15 14:30:00
Unit=one-time-task.service


Managing Timers

Use systemctl to enable, start, or check timer status:

# Enable the timer to persist across reboots
sudo systemctl enable example.timer

# Start the timer immediately
sudo systemctl start example.timer

# List all active timers
systemctl list-timers

The list-timers command shows details like the next scheduled run time and whether the timer is active.


Key Takeaways

  • Timer units schedule service units using OnCalendar and other timing directives.
  • Service units must be explicitly referenced in the timer file.
  • Commands like systemctl enable and list-timers are essential for managing and monitoring timers.
  • Timers abstract scheduling complexity, replacing or complementing traditional cron jobs.
  • Use cron-like syntax for OnCalendar to define daily, hourly, or custom schedules.