How to Run Scheduled Tasks on a VPS with systemd Timers Instead of Cron

How to Run VPS Tasks with systemd Timers Instead of Cron

Cron has scheduled Linux tasks for decades, and it still works. But on a modern VPS it hides failures, swallows output, and gives you no easy way to check whether last night’s backup actually ran. Running scheduled tasks with systemd timers instead of cron fixes those gaps: every run is logged, every failure is visible, and every timer has a clear on and off switch.

You need a VPS running systemd, which covers Ubuntu 16.04 and newer and Debian 8 and newer. You also need sudo access and a text editor.

Why systemd Timers Instead of Cron for Scheduled VPS Tasks

Cron runs a command at a set time and sends any output by email, if mail is set up at all. If mail is not set up, the output vanishes. If the job fails, cron does not retry it, does not alert you, and does not record the failure anywhere you would look first. You find out days later, when a backup is missing or a certificate has expired.

A systemd timer works differently. It triggers a service unit, and that service runs your command as a normal systemd service. The output goes to the journal, so journalctl shows exactly what happened. A cron vs systemd comparison comes down to this: cron is a scheduler with no memory, while a timer is a scheduler tied to a service that keeps a record.

Timers also handle missed runs better. If the VPS was off when a job was due, cron skips it silently. A timer with Persistent=true runs the missed job once at boot, which is what you want for a daily backup or a weekly cleanup.

How a Timer and a Service Work Together

A timer never runs a command directly. It has two files: a .timer file that says when to run, and a .service file that says what to run. The names must match, apart from the ending. A timer called backup.timer triggers a service called backup.service.

The systemd service unit holds the command, the user it runs as, and the working directory. The systemd timer unit holds the schedule.

Both files live in /etc/systemd/system/. After you create or edit them, run systemctl daemon-reload, then enable and start the timer. Enable the timer, not the service.

Write Your First systemd Timer in Five Minutes

Start with a simple job: a script that writes the date to a log file once an hour. Create the service file:

sudo nano /etc/systemd/system/hourly-note.service
[Unit]
Description=Write an hourly note to a log file

[Service]
Type=oneshot
ExecStart=/bin/bash -c 'date >> /var/log/hourly-note.log'

Type=oneshot means the service runs once and exits. Now create the timer file:

sudo nano /etc/systemd/system/hourly-note.timer
[Unit]
Description=Run the hourly note every hour

[Timer]
OnCalendar=hourly
Persistent=true

[Install]
WantedBy=timers.target

Load the files and start the timer:

sudo systemctl daemon-reload
sudo systemctl enable --now hourly-note.timer

Check when it runs next:

systemctl list-timers --all

list-timers shows every timer, its next run, and its last run in a single table. It is the first thing to check when a job seems to have stopped.

Move a Cron Job to a Timer, Step by Step

Take a real example: a MySQL backup that cron runs every night at 2 in the morning. The cron line looks like this:

Create the service file:

sudo nano /etc/systemd/system/mysql-backup.service
[Unit]
Description=Back up MySQL databases
After=network.target mysql.service

[Service]
Type=oneshot
User=root
ExecStart=/usr/local/bin/backup-mysql.sh

After=mysql.service makes sure MySQL is up before the backup starts. Cron has no equivalent: a cron job that runs during a database restart simply fails. If you do not have a backup script yet, our guide on how to back up MySQL databases automatically with mysqldump and cron shows how to write one.

Create the timer file:

sudo nano /etc/systemd/system/mysql-backup.timer
[Unit]
Description=Run the MySQL backup every night

[Timer]
OnCalendar=02:00
RandomizedDelaySec=10min
Persistent=true

[Install]
WantedBy=timers.target

RandomizedDelaySec=10min spreads the start time by up to ten minutes, so the backup does not collide with other jobs set for 2 in the morning. Then load and start it:

sudo systemctl daemon-reload
sudo systemctl enable --now mysql-backup.timer

Test the job right away without waiting for 2 in the morning:

sudo systemctl start mysql-backup.service

Starting the service directly runs the backup once, outside the schedule. Check the result with systemctl status mysql-backup.service.

Only remove the old cron line after the timer has run on schedule at least once and the backup file looks right.

OnCalendar Schedules You Will Actually Use

OnCalendar covers everything cron schedules cover, with a more readable syntax. These are the forms most VPS owners need:

  • OnCalendar=minutely runs every minute.
  • OnCalendar=hourly runs at the start of every hour.
  • OnCalendar=daily runs at midnight.
  • OnCalendar=02:30 runs every day at half past two in the morning.
  • OnCalendar=Mon *-*-* 03:00 runs every Monday at 3 in the morning.
  • OnCalendar=*-*-01 04:00 runs on the first day of every month at 4 in the morning.
  • OnCalendar=*:0/15 runs every fifteen minutes.

Check a schedule before you trust it. systemd-analyze calendar shows the next few times a schedule will fire:

systemd-analyze calendar "Mon *-*-* 03:00"

That one command has caught more scheduling mistakes than any amount of staring at a timer file.

See Logs and Failures Without Guessing

This is where timers earn their keep. To see what the last backup run printed, read its journal:

journalctl -u mysql-backup.service -n 50

To find out why a timer did not fire, check the timer itself:

systemctl status mysql-backup.timer
systemctl list-timers mysql-backup.timer

A timer can be enabled but not active if the system never reloaded after an edit. Run systemctl daemon-reload after every change to a timer or service file.

For jobs that must alert you on failure, add OnFailure to the service file and point it at a small alert service or a mail script. You can also watch failures with a monitoring tool. Our guide on installing Uptime Kuma with Docker for monitoring covers a simple way to watch services from outside the server.

Common Timer Mistakes and How to Fix Them

First, the service runs but the timer never fires. Check that the timer is enabled and that its name matches the service name exactly. A timer called backup.timer looks for backup.service, not backups.service.

Second, the job runs as the wrong user. A service without a User line runs as root, which is fine for a backup but wrong for a script that writes files a web user must read. Set User and Group in the service file.

Third, the job cannot find a command. A service runs with a minimal environment, not your shell, so a bare mysqldump or rsync may not be on its path. Use full paths in scripts run by timers, such as /usr/bin/mysqldump.

Fourth, the job runs twice. This happens when a cron line and a timer both schedule the same script. Remove or comment out the cron line once the timer is proven, and check crontab -l for leftover lines.

Fifth, the job takes longer than the gap between runs. A backup that takes 70 minutes with an hourly timer will overlap with its own next run. Set a longer gap so each run finishes before the next one starts.

When Cron Is Still the Better Choice

Timers are not better for every job. Cron wins when the task is a single line, when the server is minimal, or when the job must run for a user with no systemd access. A one-line cache clear in a user’s crontab is simpler than two unit files.

Cron also wins for jobs that must run inside a container or on a system without systemd. Some minimal images and older systems have no systemd at all, and a timer file there does nothing.

Start with one job, a backup or a cleanup you already run in cron. Move it to a timer, watch it run on schedule, read its logs, and keep the old cron line commented out for a week. Once that first timer proves itself, the rest of your scheduled jobs are the same pattern repeated: one service file, one timer file, and a log you can actually read. Jobs that must never fail silently, such as certificate renewals, belong in this group, as our guide on how to renew Let’s Encrypt certificates automatically and avoid expiry downtime shows.

FAQ

Do systemd timers replace cron completely?

No. Timers fit jobs where logging and failure alerts matter. Simple one-line jobs can stay in cron.

How do I run a timer job right now for testing?

Start its service directly with systemctl start name.service. That runs the job once without changing the schedule.

Why is my timer enabled but not running?

Check that the timer name matches the service name, run systemctl daemon-reload after edits, and confirm the timer shows as active with systemctl list-timers.

Where do timer logs go?

Output from the service goes to the systemd journal. Read it with journalctl -u name.service.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.