Skip to content

Configuring Timers

Systemd timers provide a powerful mechanism to schedule services to run at specific times or intervals. By combining .timer unit files with corresponding .service units, you can automate recurring tasks such as backups, system checks, or maintenance routines. This section demonstrates how to create, manage, and troubleshoot systemd timers.


Understanding Timer Unit Files

A .timer unit file defines when a service should be triggered. It includes two main sections: - [Unit]: Describes the timer's purpose and dependencies. - [Timer]: Specifies scheduling rules, such as intervals or calendar-based triggers.

Example structure:

[Unit]
Description=Daily System Check

[Timer]
OnCalendar=daily
Unit=syscheck.service

[Install]
WantedBy=timers.target

Key timer options in the [Timer] section: - OnCalendar: Schedule based on dates/times (e.g., daily, * * * * * for cron-like syntax). - OnUnitActiveSec: Trigger after a service has been active for a duration. - OnBootSec: Trigger after a delay following system boot. - AccuracySec: Define how precisely the timer should align with the schedule.


Creating a Timer

  1. Define the service: Create a .service file for the task. For example, a script that logs system metrics:

    [Unit]
    Description=System Metrics Logger
    
    [Service]
    ExecStart=/usr/local/bin/log_metrics.sh
    Type=oneshot
    

  2. Create the timer: Write a .timer file to schedule the service. To run the service every hour:

    [Unit]
    Description=Run System Metrics Logger Hourly
    
    [Timer]
    OnCalendar=*:*:*:0/1
    Unit=sysmetrics.service
    AccuracySec=1h
    
    [Install]
    WantedBy=timers.target
    

  3. Enable and start the timer:

    sudo systemctl enable sysmetrics.timer
    sudo systemctl start sysmetrics.timer
    

The AccuracySec option ensures the timer aligns with the specified interval, even if delayed.


Managing Timers

Use systemctl to interact with timers: - List active timers:

systemctl list-timers
Output includes the next scheduled run time and status.

  • Trigger a timer immediately:

    sudo systemctl start sysmetrics.timer
    
    Use --now to force execution without waiting for the next scheduled time:
    sudo systemctl --now start sysmetrics.timer
    

  • Modify a timer: Edit the .timer file and reload systemd:

    sudo systemctl daemon-reload
    sudo systemctl restart sysmetrics.timer
    

  • Disable or stop a timer:

    sudo systemctl disable sysmetrics.timer
    sudo systemctl stop sysmetrics.timer
    


Troubleshooting Timers

Common issues and solutions: - Timer not triggering: Check logs with:

journalctl -u sysmetrics.timer
Ensure the service unit (sysmetrics.service) exists and is valid. - Incorrect scheduling: Verify OnCalendar syntax matches systemd's format (e.g., * * * * * for every minute). - Permissions: Ensure the timer unit file is owned by root and has correct permissions (-rw-r--r--).


Key takeaways

  • Timers link to services: A .timer unit must reference a valid .service unit.
  • Use OnCalendar for flexibility: Cron-like syntax enables precise scheduling.
  • Monitor with systemctl list-timers: Track active and pending timer events.
  • Debug with journalctl: Logs reveal errors in timer or service execution.