In this lesson, you will learn how to transition from traditional cron scheduling to systemd timers, which use monotonic clocks to ensure your tasks run reliably relative to system events.
While cron is excellent for wall-clock scheduling (like "at 3:00 AM every day"), it struggles with tasks that need to run at specific intervals since the system booted or since the task last completed. This is where systemd.timer units excel. A "monotonic" timer tracks the time elapsed on a continuous clock that is not affected by system time adjustments (like NTP syncs or timezone shifts).
Understanding Monotonic Intervals
When defining a timer, you have two primary ways to track the interval:
OnBootSec=: This triggers a task a set amount of time after the system has booted. This is ideal for background services that need to start only after the network stack is fully initialized, rather than at a precise timestamp.OnUnitActiveSec=: This triggers a task relative to when the service unit was last activated. If your task takes 5 minutes to run and you want it to run every 10 minutes, this ensures the interval is calculated from the start of the previous run, preventing drift.
Configuring the Timer
To implement this, you pair a .service file with a .timer file. Here is a snippet of how a monotonic timer looks in a timer unit file:
[Timer]
# Run 5 minutes after boot
OnBootSec=5min
# Run every hour after the previous execution
OnUnitActiveSec=1h
# Ensure the timer persists across reboots
AccuracySec=1min
[Install]
WantedBy=timers.target
Unlike cron, which checks the calendar, these monotonic timers are managed by the systemd event loop. If the system is powered down when a task is scheduled to run, systemd can use the Persistent=true directive to trigger the task immediately upon the next boot. This provides a level of scheduling robustness that standard cron jobs simply cannot match, as they would simply skip the missed interval entirely.
By moving to these timers, you eliminate the "jitter" associated with wall-clock time and gain deeper integration with system state events.
Key Takeaway: Monotonic timers allow you to schedule tasks based on system-relative time intervals (like boot time or last execution time) rather than fixed wall-clock points, resulting in more reliable task orchestration.