Scheduling Jobs with Systemd Timers in Linux
Scheduling tasks in Linux can be a lifesaver when you want things done automatically. For many, cron is the go-to tool for scheduling, but systemd offers a newer alternative with more flexibility and control: systemd timers. In this guide, you’ll learn what systemd timers are, how they work, and how to create and manage them.
What are Systemd Timers?
In Linux, systemd timers provide a way to schedule tasks similar to cron but with more advanced features and integration into the systemd ecosystem. While cron schedules tasks purely on time intervals, systemd timers allow additional conditions like dependencies, service integration, and event-based triggers. This makes systemd timers a powerful tool for managing system tasks efficiently.
The Components of Systemd Timers
A systemd timer consists of two main parts: a timer unit and a service unit.
Timer Unit: This file defines when the task should run. It specifies the timing parameters, such as
OnCalendarfor calendar-based schedules andOnBootSecfor intervals after boot time.Service Unit: This file defines the actual task that will be executed. It’s where you specify what the timer is triggering, whether it’s running a script, restarting a service, or performing a cleanup operation.
These two units work together, with the timer determining the schedule and the service handling the task execution.
Why Use Systemd Timers Over Cron?
While cron is great for simple, time-based scheduling, systemd timers offer additional features that make them ideal for certain tasks:
Better Integration with Systemd:
systemd timerscan interact with othersystemdunits, making it easier to coordinate tasks with service dependencies.Event-Based Scheduling: In addition to time-based scheduling,
systemd timerssupport event-based scheduling with directives likeOnBootSec, which lets you run tasks a specific time after boot, orOnUnitActiveSec, which schedules tasks relative to other units.Persistent Scheduling: If a task is missed (for example, due to system downtime),
systemd timerscan be set to catch up automatically.
The following table shows the differences between systemd timers and cronjobs:
Creating a Systemd Timer
To demonstrate how systemd timers work, let’s create a simple example where a timer logs the current date and time to a file every 10 minutes. This is a straightforward way to set up a repeating task and see systemd timers in action.
Step 1: Create the Service Unit
The service unit defines the actual task. In this example, we’ll write the current date and time to a log file (/tmp/date).
Open a new service file in your preferred editor:
$ sudo nano /etc/systemd/system/date.serviceAdd the following content to specify the task:
[Unit]
Description=Log current date to /tmp/date
[Service]
Type=oneshot
ExecStart=/bin/sh -c ‘date >> /tmp/date’
Description: A brief description of what the service does.
Type=oneshot: Indicates that the task should run once each time it’s triggered.ExecStart: The command to execute. Here, we’re appending the current date to/tmp/datewith each run.
Step 2: Create the Timer Unit
Now, we need a timer unit that schedules when to run date.service.
Create a new timer file:
$ sudo nano /etc/systemd/system/date.timerAdd the following content to define the timing parameters:
[Unit]
Description=Run date.service every 10 minutes
[Timer]
OnCalendar=*:0/10
Persistent=true
[Install]
WantedBy=timers.target
Here’s what each directive does:
OnCalendar=*:0/10: Runs the service every 10 minutes. The syntax:0/10means it triggers at every hour, at minutes 0, 10, 20, etc.
The
OnCalendarderivative supports various time expressions, such asdaily,weekly,hourly, or even specific dates and times.
Persistent=true: Ensures any missed executions (e.g., due to system downtime) run at the next opportunity.
WantedBy=timers.target: Sets up the timer to start automatically at boot.
Sysxplore is an indie, reader-supported publication.
I break down complex technical concepts in a straightforward way, making them easy to grasp. A lot of research goes into every piece to ensure the information you read is as accurate and practical as possible.
To support my work, consider becoming a free or paid subscriber and join the growing community of tech professionals.
Step 3: Enable and Start the Timer
Once both units are defined, reload systemd to recognize them and start the timer.
Reload the systemd daemon:
$ sudo systemctl daemon-reloadEnable and start the timer:
$ sudo systemctl enable --now date.timer
$ sudo systemctl start date.timerEnabling the timer ensures it starts at boot, and starting it now initiates the 10-minute schedule without needing a reboot.
Step 4: Checking the Timer Status
To view the status of a timer and see when it last ran and when it’s scheduled to run next, use:
$ sudo systemctl list-timers --allThis command provides a list of all active timers, including system timers, allowing you to keep track of their schedules and confirm they’re running as expected.
You can also get specific details of a timer with:
$ sudo systemctl status date.timerThis command provides information on whether the timer is running and when it will run next, helping you verify that everything is set up correctly. If you look at /tmp/date, you should start seeing timestamps every 10 minutes, confirming that your timer is working as expected.
And that’s it! You’ve set up a systemd timer to run a task at regular intervals, giving you a useful alternative to cron for managing scheduled tasks on your Linux system.
Step 5: Stopping and Disabling a Timer
If you need to stop or disable a timer, it’s as simple as running:
$ sudo systemctl stop date.timer
$ sudo systemctl disable date.timerStopping a timer halts its current schedule, and disabling it prevents it from starting at boot.
Understanding Timer Time Expressions
One of the highlights of systemd timers is the flexibility of the OnCalendar directive. Here’s a quick overview of some common expressions you can use:
daily: Runs once every day at midnight.hourly: Runs at the top of every hour.weekly: Runs every Monday at midnight.OnCalendar=*-*-1 00:00:00: Runs at midnight on the first day of each month.Custom Times: You can also specify complex schedules, like
Mon *-1..7for running the timer only on Mondays in the first week of each month.
This flexibility is particularly helpful when you have tasks with non-standard scheduling needs.
Advanced Features of systemd Timers
Accuracy Settings: By default, timers may not execute at the exact specified time but within a variance defined by
AccuracySec. This can be adjusted for precise scheduling.Transient Timers: These are temporary timers that exist only for the current session and can be created using
systemd-run.Logging and Monitoring: Timers integrate with system logging, allowing you to monitor their execution history easily.
Thanks for reading!
If you enjoyed this content, don’t forget to leave a comment, like ❤️ and subscribe to get more posts like this every week.






