📢 LordCloud support has moved to Ballastra — Join now →
DevOps & Engineering

Cron vs Systemd Timer: Which Should Run Your Scheduled Tasks?

Both run commands on a schedule, but they fail in different ways. Here is when plain cron is enough and when a systemd timer saves you a 3 a.m. surprise.

LordCloud Engineering

Infrastructure & Support Team

Published · 6 min read
On this page

If you are choosing between cron vs systemd timer for a backup, a log cleanup or a certificate renewal, the short answer is this: use cron for simple, disposable jobs, and use a systemd timer when you need logs, catch-up after downtime, dependencies or resource limits. Both are installed on practically every modern distribution, and they can live side by side.

The difference only becomes obvious when something goes wrong. A cron job that fails mails a message nobody reads, or nothing at all. A systemd timer that fails leaves a status you can query and a log you can search. This guide walks through the practical differences on a Linux VPS, with examples you can paste and test.

How cron works, and where it bites#

Cron reads a table of schedules. Each line has five time fields (minute, hour, day of month, month, day of week) followed by the command. Edit your own table with crontab -e:

text
# m  h  dom mon dow  command
30 2 * * *  /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

That runs at 02:30 every day. Cron is tiny, has been around for decades, and the syntax is easy to remember. The manual page for crontab(5) documents a few behaviours that catch people out in practice:

  • SHELL defaults to /bin/sh, not bash, so bash-only syntax in a one-liner can fail silently.
  • A % in the command is turned into a newline. Everything after the first one is fed to the command as standard input. Escape it as \%, which matters for date +%F style filenames.
  • If both day-of-month and day-of-week are restricted, the job runs when either matches, not both.
  • Daylight saving changes: times skipped by a clock change never match, and times that occur twice run twice. This rarely matters in India, which has no DST, but it matters if your server is set to a UTC-offset zone that does.
  • Mail: output goes by mail to the crontab owner unless MAILTO is set. Set MAILTO="" and you get no mail at all, so make sure you redirect output yourself.

There is also no built-in memory of a run. If the server was rebooted at 02:25 and came back at 02:40, the 02:30 job is gone.

Why is my cron job not running?#

Nine times out of ten the script works in your shell and fails in cron because cron does not give it your login environment. Use absolute paths for every binary and file, set PATH at the top of the crontab if you must, and send output to a file while debugging:

text
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
30 2 * * *  /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1

Then check the system log for the cron daemon (on Debian and Ubuntu, grep CRON /var/log/syslog or journalctl -u cron; the service name differs on other distributions). If the line never shows up, the problem is the schedule or the file format, not your script.

How a systemd timer works#

A timer is a pair of unit files: a .service that describes what to run, and a .timer that describes when. You enable and start the timer, and systemd starts the service when the schedule elapses.

Create /etc/systemd/system/backup.service:

text
[Unit]
Description=Nightly backup

[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh

And /etc/systemd/system/backup.timer:

text
[Unit]
Description=Run backup every night

[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true

[Install]
WantedBy=timers.target

Then load and enable it:

text
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers --all

list-timers shows when each timer last ran and when it runs next, which is something cron simply cannot tell you. Run the job once by hand with sudo systemctl start backup.service to confirm the service itself works before you wait for the schedule.

Systemd timer persistent: catching up after downtime#

The setting that wins most people over is Persistent=. According to the systemd.timer(5) manual, when it is true the last trigger time is stored on disk, so a run that was missed while the machine was off fires when the timer is activated again. It defaults to false and only affects OnCalendar= timers, so you must opt in.

For a VPS that you reboot for kernel updates, or a small home or office box that is switched off at night, this is the difference between a backup that happens and one that quietly never does.

Spreading load with RandomizedDelaySec#

If you run the same job on many servers, or you do not want a heavy task to start exactly on the hour alongside everything else, add RandomizedDelaySec=15min. The manual describes it as a random, evenly distributed delay between 0 and the value you give. Do not confuse it with AccuracySec=, which defaults to one minute and lets systemd coalesce wake-ups; set AccuracySec=1s if you care about the exact second.

Cron vs systemd timer: side by side#

Need Cron Systemd timer
One-line job, minimal setup Excellent Two files per job
Logs Mail or your own redirect journalctl -u backup.service
Catch up after downtime No Yes, with Persistent=true
See next and last run Not built in systemctl list-timers
Run after network or a mount is ready Awkward After= and Requires= in the service
Limit CPU, memory or I/O Wrap with nice/ionice Resource-control settings in the service
Randomised start ~ ranges, picked at parse time RandomizedDelaySec=
Works on non-systemd systems Yes No

A useful side effect of the service model: systemd will not start a second instance of a service that is still running when the timer fires again. With cron, a slow job and a short interval give you overlapping runs, which is how people end up with five copies of a backup fighting over the same disk. If you stay on cron, guard the script with flock:

text
*/10 * * * *  /usr/bin/flock -n /tmp/sync.lock /usr/local/bin/sync.sh

Writing and testing the schedule#

Systemd uses its own calendar syntax, documented in systemd.time(7). Missing date parts default to *-*-* and missing time parts to zero, so 05:40 means every day at 05:40:00. There are shorthands such as daily, weekly and monthly, ranges with .., and lists with commas. For example, Mon..Fri 09:00 runs on weekdays at nine.

Never guess; check it:

text
systemd-analyze calendar "Mon..Fri 09:00"

The command prints the normalised expression and when it will next elapse. For a six-hourly job, OnCalendar=*-*-* 00/6:00:00 is checkable in the same way.

Schedules in IST#

Most VPS images default to UTC. A job that you think runs at 02:30 IST will run at 08:00 IST if the server clock is UTC. You can append a timezone to a systemd calendar expression, for example OnCalendar=*-*-* 02:30:00 Asia/Kolkata, or change the server timezone with timedatectl set-timezone Asia/Kolkata. Check timedatectl first; do not assume. In cron, support for a per-crontab timezone variable depends on the cron implementation, so the safe approach is to know the server timezone and convert.

Whichever you pick, schedule heavy jobs for hours when your own traffic is low. For most Indian audiences that is roughly 02:00 to 05:00 IST.

Which one should you use?#

My rule of thumb after running both for years:

  1. Use cron for quick personal jobs, anything that must work on a non-systemd or very old box, and tasks where skipping a run is harmless (clearing a cache folder, say).
  2. Use a systemd timer for anything you would be upset to find missing: backups, renewals, database dumps, report generation. The logging and Persistent= alone justify the extra file.
  3. Do not migrate everything. Packages on Debian and Ubuntu ship their own cron.daily jobs or timers; leave those alone.

Whichever scheduler you use, a scheduled job that nobody checks is a liability. Pair it with basic monitoring (see our notes on server monitoring), keep a copy of backups off the machine, and remember that a hardened box needs its own protections as well; our Fail2ban SSH setup guide covers the login side. If you are new to the server itself, start with connecting through SSH.

A complete example: a database dump with logging#

Here is a realistic job as a timer pair. The service writes a dump and exits non-zero on failure, which systemd records:

text
# /etc/systemd/system/db-dump.service
[Unit]
Description=Dump the application database
After=network-online.target

[Service]
Type=oneshot
User=backup
ExecStart=/usr/local/bin/db-dump.sh
Nice=10
IOSchedulingClass=idle
text
# /etc/systemd/system/db-dump.timer
[Unit]
Description=Daily database dump

[Timer]
OnCalendar=*-*-* 03:00:00 Asia/Kolkata
RandomizedDelaySec=10min
Persistent=true

[Install]
WantedBy=timers.target

After systemctl enable --now db-dump.timer, view results any day with journalctl -u db-dump.service --since yesterday. If you want a failure alert, add an OnFailure= unit that sends a message or hits a webhook, which is far harder to do cleanly from cron.

The same approach works for the maintenance jobs you will want on a VPS you control end to end. If you need a server where you can set this up with full root access, see the Linux VPS plans on LordCloud.

Frequently asked questions

Is a systemd timer better than cron?

Neither is better in every case. Systemd timers give you logs in journald, dependency handling, catch-up after downtime and resource limits. Cron is simpler, works everywhere, and is perfectly fine for a one-line job that you do not mind losing if the server was off.

Does a systemd timer run missed jobs after the server was off?

Only if you set Persistent=true on an OnCalendar= timer. The last trigger time is stored on disk and, if a run was missed, it fires when the timer is activated again. Plain cron does not catch up; a job scheduled while the machine was down is simply skipped.

Why is my cron job not running?

The usual causes are a minimal PATH and environment inside cron, a relative path in the command, an unescaped % character, a missing newline at the end of the file, or the job running in a different timezone than you assumed. Redirect output to a log file and use absolute paths to see the real error.

Can I use both cron and systemd timers on the same VPS?

Yes. They do not conflict. Many servers keep distribution-provided cron jobs for housekeeping and use systemd timers for the jobs they wrote themselves.

How do I test a systemd OnCalendar expression?

Run systemd-analyze calendar with the expression in quotes. It validates the syntax, prints the normalised form and shows when the event will next elapse.

References

Drafted with AI assistance and checked against LordCloud's published product information. Spotted a mistake?Suggest an edit.

All articles →
Have a question? Chat with us👋