systemd timers
Replace a cron job with a systemd timer and service pair.
By Invicly Tech, , in Linux Administration (RHCSA-style). About 9 minutes to read.
In short
systemd timers are the modern alternative to cron. A timer unit decides when to run and activates a matching service unit that does the work, so you get journald logging, dependencies, resource limits and catch-up for missed runs with Persistent=true.
Explained simply
A systemd timer is an alarm clock for a service. Instead of one cron line, you write two small files. The service file says what to run. The timer file says when to run it.
That looks like more work, but you get a lot for free. Every run is logged in the journal under the service name. You can see when it ran last, whether it failed, and when it runs next, all with one command.
Timers can also catch up. If the server was off at 02:30, a timer with Persistent=true runs the job as soon as the machine boots. And a timer never starts a second copy while the first one is still running.
You can schedule by the calendar, like every weekday at 06:00, or relative to an event, like 10 minutes after boot or one hour after the job last finished.
Press Play on a diagram to walk through it one step at a time.
- systemd to backup.timer: Timer started at boot (timers.target)1Timer started at boot (timers.target)
- backup.timer to backup.service: OnCalendar time reached: start the service2OnCalendar time reached: start the service
- backup.service to journald: Script output logged with the unit name3Script output logged with the unit name
- backup.service to systemd: Oneshot service exits with a status4Oneshot service exits with a status
- systemd to backup.timer: Next elapse time scheduled5Next elapse time scheduled
- 1Read the cron line
30 2 * * 1-5 /usr/local/bin/backup.sh
- 2Write backup.service
Type=oneshot, ExecStart= the script
- 3Write backup.timer
OnCalendar=Mon..Fri 02:30
- 4Check the schedule
systemd-analyze calendar
- 5Enable the timer
systemctl enable --now backup.timer
- 6Remove the cron line
So the job does not run twice
In depth
The OnCalendar language
OnCalendar uses the form DayOfWeek Year-Month-Day Hour:Minute:Second, and any part may be a star or left out. *-*-* 02:30:00 means every day at 02:30. Mon..Fri 06:00 means weekdays at 06:00. *:0/15 means every 15 minutes. Words work too: minutely, hourly, daily, weekly, monthly, yearly. daily is midnight, and weekly is Monday at 00:00.
You can list several values with commas, such as Mon,Wed,Fri 18:00, and add a time zone at the end, such as 02:30 Europe/London. Never guess: systemd-analyze calendar prints the normalized form and the next time it will fire, and --iterations=5 shows the next five.
- systemd-analyze calendar 'Mon..Fri 06:00' --iterations=3
- *-*-01 00:00:00 runs on the first of each month
- Several OnCalendar= lines in one timer are all used
Monotonic timers: relative to an event
Instead of a wall-clock time, a timer can count from an event. OnBootSec=10min fires 10 minutes after boot. OnUnitActiveSec=1h fires one hour after the service last started, and OnUnitInactiveSec=1h one hour after it last finished, which guarantees a gap between runs no matter how long each takes. Times accept units like 30s, 5min, 2h and 1d.
A common pair is OnBootSec=5min plus OnUnitActiveSec=1h: first run shortly after boot, then hourly. Persistent=true only affects OnCalendar timers, because monotonic timers already count from boot.
- Fixed clock times
- Daily at 02:30, weekdays at 06:00
- Persistent=true catches missed runs
- Follows the time zone
- Counted from an event
- 10 min after boot, then every hour
- No wall clock needed
- Spacing stays even after downtime
Accuracy, random delay and missed runs
By default systemd may start a timer up to one minute late (AccuracySec=1min) so it can group wake-ups and save power. If you need the exact second, set AccuracySec=1s. RandomizedDelaySec=30min adds a random wait, so 500 servers do not hit the backup server at the same moment.
Persistent=true stores the last run time on disk, under /var/lib/systemd/timers. At boot, if a scheduled time passed while the machine was off, the service runs right away. Without it, a missed run is simply skipped. And if the service is still running when the timer fires again, systemd does not start a second copy, so you never need lock files.
- AccuracySec=1s for exact timing
- RandomizedDelaySec spreads load across a fleet
- Unit=other.service makes a timer start a differently named unit
Writing the service part well
The service is a normal unit with Type=oneshot, meaning it runs to the end and then exits. It needs no [Install] section, because the timer starts it. Put real settings here: User=backup to drop root, After=network-online.target with Wants=network-online.target if the job needs the network, Nice=10 and IOSchedulingClass=idle to stay out of the way, and OnFailure=notify@%n.service to trigger an alert unit when it fails.
Test the service alone with systemctl start backup.service and read journalctl -u backup.service before you enable the timer. Finally, enable the timer, not the service: systemctl enable --now backup.timer.
User timers and quick transient timers
Normal users can have timers too. Put the files in ~/.config/systemd/user/ and use systemctl --user daemon-reload and systemctl --user enable --now NAME.timer. They run only while the user is logged in, unless an admin enables lingering with loginctl enable-linger USER.
For a one-off job, systemd-run can create a timer on the fly without any files. sudo systemd-run --on-active=30min /usr/bin/systemctl restart httpd runs the command in 30 minutes, and --on-calendar='2026-11-11 22:00' runs it at a set time. This is a modern alternative to at, and the run is logged in the journal like any unit.
Learn it in 4 short steps
About 30m each. Track them with a timer in your plan.
- 1Write a oneshot service~30m
- 2Write a timer with OnCalendar~30m
- 3Test expressions with systemd-analyze calendar~30m
- 4List timers with systemctl list-timers~30m
Key points
- foo.timer activates foo.service by default.
- Enable and start the timer, not the service.
- OnCalendar=*-*-* 02:30:00 means daily at 02:30; systemd-analyze calendar checks expressions.
- Persistent=true runs a missed job at the next boot.
Key terms
Tap a card to flip it. 0/5 known
Examples
# /etc/systemd/system/backup.service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
# /etc/systemd/system/backup.timer
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
[Install]
WantedBy=timers.target
# then: systemctl daemon-reload && systemctl enable --now backup.timerA nightly backup as a timer and service pair.
Clean old upload files every 6 hours, 10 minutes after boot first
A web app leaves temp uploads in /srv/app/tmp. Step 1: write a oneshot service that runs as the app user with low priority. Step 2: write a timer using monotonic settings: first run 10 minutes after boot, then 6 hours after each run. Step 3: reload, enable the timer, then list it to see the next run. Step 4: trigger the service once by hand and read its log.
# /etc/systemd/system/tmpclean.service
[Service]
Type=oneshot
User=app
Nice=10
ExecStart=/usr/bin/find /srv/app/tmp -type f -mmin +360 -delete
# /etc/systemd/system/tmpclean.timer
[Timer]
OnBootSec=10min
OnUnitActiveSec=6h
[Install]
WantedBy=timers.target
# systemctl daemon-reload; systemctl enable --now tmpclean.timer
# systemctl start tmpclean.service; journalctl -u tmpclean.serviceDebug a timer that never seems to fire
You wrote report.timer, but no report appears. Step 1: list timers; if NEXT shows n/a, the timer is not active or the expression never matches. Step 2: check the expression with systemd-analyze calendar. Step 3: confirm the timer is enabled and started, and that you ran daemon-reload after editing. Step 4: check the service log; the timer may be fine and the script failing.
systemctl list-timers --all | grep report
systemd-analyze calendar 'Mon *-*-* 07:00'
systemctl is-enabled report.timer; systemctl is-active report.timer
sudo systemctl daemon-reload && sudo systemctl enable --now report.timer
systemctl status report.service
journalctl -u report.service -n 20Quick reference
- foo.timer starts foo.service unless Unit= says otherwise
- Enable the timer: systemctl enable --now foo.timer
- OnCalendar=Mon..Fri 06:00, *:0/15 (every 15 min), daily, weekly
- systemd-analyze calendar 'EXPR' --iterations=5 shows the next run times
- systemctl list-timers --all shows LAST and NEXT for every timer
- Persistent=true runs a missed OnCalendar job at boot
- AccuracySec defaults to 1min; set 1s when timing must be exact
- systemd-run --on-active=30min CMD is a one-off timer with no files
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
List all timers on your machine and find which one runs next and which unit it activates.
- Exercise 2medium
Convert this cron line into a timer and service: 30 2 * * 1-5 /usr/local/bin/backup.sh. Make it catch up after downtime.
- Exercise 3hard
As a normal user, create a timer that logs the disk usage of your home every 30 minutes and keeps running after you log out.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Are systemd timers better than cron?
- They give journal logging, catch-up with Persistent=true, no overlapping runs and service settings like User= and resource limits. cron is shorter to write. Many teams use timers for new jobs and keep old cron jobs until they are migrated.
- How do I run a timer job right now for testing?
- Start the service directly with systemctl start NAME.service. The timer's schedule is not affected.
- Why does my timer show NEXT as n/a?
- The timer is not started, the expression never matches, or you forgot systemctl daemon-reload after editing. Check with systemctl status NAME.timer and systemd-analyze calendar.
- Can a timer send me email on failure like cron does?
- Not by itself. Add OnFailure= to the service pointing to a unit that sends an alert, or have the script report its own errors.