Lorixen.
EN

Moving scheduled jobs from cron to systemd timers

Short answer: systemd timers do what cron does, plus three things small teams usually want: each run's output lands in the system journal, a job that's still running isn't started a second time, and with Persistent=true a run missed while the server was off happens when it comes back. You write two small files, a .service that says what to run and a .timer that says when, then enable the timer. For a handful of important jobs on a modern Linux server, it's worth the switch.

Why move away from cron at all?

Cron is simple and reliable, but it has gaps that tend to show up at the worst moment:

  • Output goes nowhere useful. Unless you redirect it, output is mailed to a local mailbox nobody reads, or lost.
  • Overlaps. If last night's export is still running when the next one starts, cron happily starts a second copy.
  • Missed runs. If the server was rebooting or switched off at 2:00, the 2:00 job simply doesn't happen.
  • Different environment. Cron runs with a minimal environment, so scripts that work in your terminal fail on schedule.

systemd timers don't fix bad scripts, but they make each of these easier to see and control.

The two files

A timer always activates a unit. The systemd.timer documentation explains that by default foo.timer activates foo.service, and recommends naming them identically apart from the suffix.

/etc/systemd/system/order-export.service

[Unit]
Description=Nightly order export

[Service]
Type=oneshot
User=app
WorkingDirectory=/srv/shop
ExecStart=/srv/shop/bin/export-orders

/etc/systemd/system/order-export.timer

[Unit]
Description=Run order export nightly

[Timer]
OnCalendar=*-*-* 02:30:00 Australia/Sydney
Persistent=true
RandomizedDelaySec=5min

[Install]
WantedBy=timers.target

Then reload and enable:

sudo systemctl daemon-reload followed by sudo systemctl enable --now order-export.timer.

Reading OnCalendar

The systemd.time documentation describes calendar events in a weekday year-month-day hour:minute:second shape, with * as a wildcard and shorthands such as hourly, daily, weekly and monthly. A few useful patterns:

  • daily means midnight every day.
  • Mon..Fri *-*-* 07:00:00 means 7am on weekdays.
  • *-*-01 03:00:00 means 3am on the first of each month.
  • Adding a time zone name after the time, as in the example above, pins the schedule to that zone rather than the server's.

The docs also mention that systemd-analyze can validate and normalise timestamps, which is a quick way to check an expression before you rely on it.

The features that matter

No overlapping runs

The systemd.timer documentation states that if the unit to activate is already active when the timer elapses, it's not restarted but left running; there's no concept of spawning new instances. So a slow export simply delays the next run instead of stacking a second copy on top. With cron you'd need a lock file or flock to get the same behaviour.

Catching up after downtime

With Persistent=true, systemd stores when the service was last triggered. When the timer is activated again, for example after a reboot, the service runs immediately if it would have run at least once while the timer was inactive. The docs describe it as useful to catch up on missed runs.

Spreading the load

RandomizedDelaySec= delays each run by a random amount up to the value you set. If you have several servers or several jobs at 2:30, it stops them all hitting the database at the same second.

Waiting for the clock

The docs note that timers with OnCalendar= are ordered after the time-set and time-sync targets, to avoid starting before the system clock is correct. Cron jobs at boot can fire before that.

Seeing what happened

This is where most teams feel the difference day to day:

  • systemctl list-timers shows every timer, when it last ran and when it runs next.
  • systemctl status order-export.service shows whether the last run succeeded and its exit code.
  • journalctl -u order-export.service shows everything the script printed, with timestamps, across past runs.

Pair it with an alert. A common approach is an OnFailure= line in the service's [Unit] section pointing at a small notification service that emails or posts to your team chat when the job exits with an error. Combined with an external "did it run?" heartbeat check for critical jobs, you'll know about both failures and silence.

Migrating a crontab, one job at a time

  1. Pick one job and note its schedule, user, working directory and any environment variables it needs.
  2. Write the service file and run it once by hand with systemctl start. Check the journal.
  3. Write the timer, enable it, and confirm list-timers shows the next run time you expect.
  4. Comment out the cron line rather than deleting it, so you can roll back.
  5. After a few successful runs, remove the cron entry.

Keep cron for trivial jobs if you like; there's no need to move everything.

A worked example

An illustrative Adelaide homewares shop exported orders to its warehouse every night with a cron job. Twice in one month the export ran twice because the first run was slow, and the warehouse received duplicate pick lists. Another night the server was being patched at 2:30 and no export ran at all. Moving the job to a systemd timer with Persistent=true solved both: a slow run now delays the next one, and a missed run happens after the reboot. The journal also showed, for the first time, how long each export took.

FAQ

Do I need root to use timers?

System timers live in /etc/systemd/system and need admin rights. Users can also run their own timers under their account, though those depend on the user's session settings.

Can a timer run every five minutes?

Yes. Use an OnCalendar expression with a minute step, or a monotonic timer such as OnUnitActiveSec=5min.

What about shared hosting without systemd access?

Then cron, or your host's scheduler, is what you have. Add a lock and log redirection to cover the gaps.

Does this work in Docker containers?

Containers usually don't run systemd. Schedule jobs from the host, the orchestrator or a dedicated scheduler instead.

If your scheduled jobs overlap, go missing or fail silently, send us your crontab and we'll suggest which ones to move and how.

Sources

WhatsAppBook a call