In this lesson, you will learn how to move beyond basic periodic execution by using systemd calendar timers to trigger tasks based on complex, real-world date and time logic.
While monotonic timers (which trigger every X seconds or minutes) are great for background upkeep, they struggle with calendar-aware requests, such as "run this task on the last Friday of every month" or "at 3:00 AM on the first day of the year." systemd addresses this through its OnCalendar= directive, which supports an expressive syntax for describing precise schedule criteria.
Defining a Calendar Timer
To implement this, you define a .timer unit file. Inside the [Timer] section, the OnCalendar= setting replaces the rigid fields found in traditional cron syntax.
Consider a scenario where you need to run a cleanup script at 02:00 every Monday and Thursday:
[Unit]
Description=Bi-weekly Cleanup Timer
[Timer]
# Syntax: DayOfWeek Year-Month-Day Hour:Minute:Second
OnCalendar=Mon,Thu *-*-* 02:00:00
AccuracySec=1us
Persistent=true
[Install]
WantedBy=timers.target
Why Use Calendar Timers?
Unlike traditional scheduling, systemd calendar timers treat the system clock as a source of truth. By setting AccuracySec=1us (or a small value), you ensure that the system wakes up precisely at the requested moment, providing the "real-time" responsiveness required for tasks that must align with specific human-readable timestamps.
Furthermore, by setting Persistent=true, you solve a common automation headache: if the system was powered off during the scheduled time, systemd will trigger the job immediately upon boot. This ensures your tasks remain synchronized with the calendar even after maintenance or unexpected outages.
This approach integrates perfectly with your existing knowledge of systemd units, allowing you to manage dependencies and execution environments far more reliably than traditional cron jobs. As you build your automation suite, prioritize these calendar specifications to ensure your background tasks respect business hours, workdays, and specific organizational events.
Key Takeaway: Use OnCalendar= directives in systemd timer units to achieve precise, persistent, and human-readable scheduling that overcomes the rigid limitations of traditional interval-based cron tasks.