
Systemd Timers Cron Alternative
- 5 minsA Beginner’s Guide to Systemd Timers
Introduction
Systemd timers offer a modern and flexible way to schedule automated tasks in Linux. Unlike traditional cron jobs, systemd timers integrate directly with systemd services, providing enhanced logging, better dependency management, and improved error handling.
We can use systemd timers insteaed of cron
In this guide, you’ll learn how to create and manage systemd timers with practical examples.
What Are Systemd Timers?
A systemd timer is a unit file in systemd that schedules tasks at specific intervals or times. It serves as a replacement for cron while offering:
-
Centralized logging and monitoring via
journalctl
- Tighter integration with systemd services
- Flexible scheduling options using time-based and calendar-based triggers
- Improved error handling and dependency management
To list all available timers on your system, run:
systemctl list-timers --all
or
systemctl status *timer
How Systemd Timers Work
Systemd timers consist of two main unit files:
- Timer Unit File – Defines when a task should run.
- Service Unit File – Specifies what task should be executed.
For example, to run a Memory check script every minutes:
- A service file defines the backup script execution.
- A timer file schedules the service execution at a specific time.
You can control timers using standard systemctl commands:
systemctl start mytimer.service
systemctl stop mytimer.service
systemctl enable mytimer.timer
systemctl status mytimer.timer
Understanding Systemd Timer Expressions
Systemd timers use a structured time format:
* *-*-* *:*:*
│ │ │ │ │ │ │
│ │ │ │ │ │ └──── Seconds (0 - 59)
│ │ │ │ │ └────── Minutes (0 - 59)
│ │ │ │ └──────── Hours (0 - 24)
│ │ │ └────────── Day of Month (1 - 31)
│ │ └──────────── Month (1 - 12)
│ └────────────── Year
└──────────────── Day of Week (Sun - Sat)
Example Expressions
Schedule | Systemd Timer Expression | Description |
---|---|---|
Every 10 minutes | *-*-* *:0/10:00 | Runs every 10 minutes |
Every Monday at 8 AM | Mon *-*-* 08:00:00 | Runs every Monday at 8 AM |
First of every month | *-*-01 00:00:00 | Runs at midnight on the 1st of each month |
Every weekday at noon | Mon..Fri *-*-* 12:00:00 | Runs at noon from Monday to Friday |
Creating a Systemd Timer
- Step 1: Create a Service File
- First, create a service file to define the task.
- I will use free command as an example and I will check free memory on the box
- For systemd service creation You need to create unit file
vi /etc/systemd/system/myMemoryMonitor.service
- Let’s Check status of service
systemctl status myMemoryMonitor.service
- Let’s start the sevice
systemctl start myMemoryMonitor.service
- As You can see above output myMmyMemoryMonitor Service started and finished after free command ran.
- We can check journal logs to see more detailed service logs.
journalctl -S today -u myMemoryMonitor.service
-
Step 2: Create the Timer File
- Now, create a timer file to schedule the service execution.
- We need to create a new file myMemoryMonitor.timer (same name of service with .timer) , in /etc/systemd/system
vi /etc/systemd/system/myMemoryMonitor.timer
- We can enable timer service
systemctl start myMemoryMonitor.timer
- We can wtach logs real time to see what services will do.
journalctl -S today -f -u myMemoryMonitor.service
- If you start timer myMemoryMonitor.service will run every minutes.
Step 3: Enable and Start the Timer
- Once We have verified We are ready to enable timer service
- Reload systemd, enable, and start the timer:
sudo systemctl daemon-reload
sudo systemctl enable myMemoryMonitor.service
sudo systemctl start myMemoryMonitor.service
- Let’s verify timer service added to list-timers
systemctl list-timers --all
Conclusion
Systemd timers provide a robust, flexible alternative to cron jobs with better logging and service management. By learning to use systemd timers, you can automate tasks efficiently while maintaining system stability.
You can also check out this article: https://coady.tech/systemd-timer-vs-cron/)
Thanks for reading…
Guneycan Sanli.