Systemd timers are a powerful alternative to cron jobs for scheduling tasks. They offer greater flexibility and better integration into the systemd ecosystem. In this article, I’ll show you how to set up and use systemd timers.
What is a systemd timer?
A systemd timer is a systemd unit that schedules another service to run at specific times. Timers replace cron jobs and offer advantages such as:
- Better logging with
journalctl. - The ability to define dependencies and startup conditions.
- More precise control over execution times.
Basic Structure
A systemd timer consists of two files:
- Service file: Defines what should be executed.
- Timer file: Defines when the service should be executed.
Both files are stored in the /etc/systemd/system/ directory.
Step 1: Create the Service File
The service file describes the action to be executed. For example, let’s run a script located at /usr/local/bin/backup.sh.
Create a file named backup.service:
| |
Content of the file:
| |
Explanation:
[Unit]: Describes the unit.Descriptionprovides a short description, andAfter=network.targetensures the network is available before the service starts.[Service]: Defines the service.Type=oneshotmeans the service performs a one-time action.ExecStartspecifies the command or script to be executed.
Step 2: Create the Timer File
The timer file defines when the service should be executed. Create a file named backup.timer:
| |
Content of the file:
| |
Explanation:
[Unit]: Similar to the service file, this provides a short description.[Timer]: Defines the scheduling.OnCalendar=daily: Runs the service daily at midnight. Other time specifications can also be used (see below).Persistent=true: Ensures the timer runs after a restart, even if it was missed during downtime.
[Install]:WantedBy=timers.targetensures the timer is activated at system startup.
Time Specifications for systemd Timers
The time for a systemd timer is defined in the timer file using the OnCalendar option. Systemd supports a flexible and powerful time format for both simple and complex schedules. Here are some examples and explanations:
Basic Format
The general format for OnCalendar is:
| |
- YYYY: Year (e.g., 2025)
- MM: Month (01 to 12)
- DD: Day (01 to 31)
- HH: Hour (00 to 23)
- MM: Minute (00 to 59)
- SS: Second (00 to 59)
You can use wildcards (*) to make certain parts flexible.
Common Time Specifications
Hourly (every full hour):
1OnCalendar=hourlyEquivalent to:
*-*-* *:00:00Daily at midnight:
1OnCalendar=dailyEquivalent to:
*-*-* 00:00:00Weekly (Sunday at midnight):
1OnCalendar=weeklyEquivalent to:
Sun *-*-* 00:00:00Monthly (1st day of the month at midnight):
1OnCalendar=monthlyEquivalent to:
*-*-01 00:00:00Yearly (January 1st at midnight):
1OnCalendar=yearlyEquivalent to:
*-01-01 00:00:00
Custom Time Specifications
Every day at 15:30:
1OnCalendar=*-*-* 15:30:00Every Monday at 08:00:
1OnCalendar=Mon *-*-* 08:00:00Every first day of the month at 12:00:
1OnCalendar=*-*-01 12:00:00Every last day of the month at 23:59:
1OnCalendar=*-*-28..31 23:59:00(Systemd automatically detects the last day of the month, e.g., 28, 30, or 31.)
Every second day at 06:00:
1OnCalendar=*-*-1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31 06:00:00
Repetitions and Intervals
Every 5 minutes:
1OnCalendar=*:0/5(Runs at minute 0, 5, 10, 15, etc., every hour.)
Every 2 hours:
1OnCalendar=*-*-* 0/2:00:00(Runs at 00:00, 02:00, 04:00, etc.)
Every 15 minutes:
1OnCalendar=*:0/15(Runs at 00, 15, 30, and 45 minutes past the hour.)
Every 10 days:
1OnCalendar=*-*-01,11,21 00:00:00
Step 3: Reload Systemd
After creating the files, reload the systemd configuration:
| |
Step 4: Enable and Start the Timer
Enable the timer so it starts automatically at system boot:
| |
If you want to start it manually:
| |
Step 5: Verify the Timer
To ensure the timer is set up correctly, check its status:
| |
And to check the service:
| |
Optional Commands and Tips
List all timers:
1systemctl list-timers --allThis displays all active and inactive timers, including their next and last execution times.
Check service status:
1systemctl status backup.serviceView service logs:
1journalctl -u backup.serviceStop or disable a timer:
- Stop the timer:
1systemctl stop backup.timer - Disable the timer:
1systemctl disable backup.timer
- Stop the timer:
Conclusion
Systemd timers are a modern and powerful alternative to cron jobs. They offer precise control, better logging, and seamless integration into the system. With the steps described above, you can easily set up and manage your own timers.