Log inSign up

Run tasks on a schedule with cron or systemd timers

For a quick recurring task, run crontab -e and add a line such as 15 3 * * * /home/deploy/bin/backup.sh to run a script every night at 03:15. For jobs that need logs in the journal, catch-up after downtime or random delays, pair a .service file with a .timer file that has OnCalendar=*-*-* 03:15:00 and Persistent=true, then enable the timer.

Topic: Server basics · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9 · Updated 2026-12-03

Should I use cron or a systemd timer?

Pick cron for short, simple jobs you want to set up in one line, and a systemd timer when you want proper logs, resource limits or runs that catch up after the server was down. Both are reliable. cron is older and familiar; every distribution understands the same five-field syntax. Timers take two small files, but each run becomes a normal systemd service, so journalctl, systemctl status, sandboxing and memory caps all apply.

A good rule: personal scripts and quick maintenance go in a crontab, while anything that matters to production (database dumps, certificate renewal hooks, report generation) goes in a timer next to the service units you already manage.

Is cron installed and running?

Check the daemon before writing jobs. Debian and Ubuntu call the package cron, while Rocky Linux and AlmaLinux use cronie with a service named crond. Minimal images sometimes leave it out:

# Ubuntu and Debian
sudo apt install cron
systemctl status cron

# Rocky Linux and AlmaLinux
sudo dnf install cronie
sudo systemctl enable --now crond
systemctl status crond

cron uses the system time zone, so confirm it with timedatectl first. A server set to UTC runs a 03:15 job at 03:15 UTC, which may be the middle of your working day. The time zone guide shows how to change it.

How do I read the crontab time fields?

Each line has five time fields followed by the command: minute, hour, day of month, month and day of week. An asterisk means every value, a comma lists values, a dash gives a range and a slash sets a step:

# m   h   dom mon dow  command
*/10  *   *   *   *    /home/deploy/bin/check.sh
0     */6 *   *   *    /home/deploy/bin/sync.sh
30    2   *   *   1-5  /home/deploy/bin/report.sh
0     4   1   *   *    /home/deploy/bin/monthly.sh
@reboot                /home/deploy/bin/on-boot.sh

The lines above run every ten minutes, every six hours on the hour, at 02:30 on weekdays, at 04:00 on the first of each month, and once after each boot. Day of week runs from 0 (Sunday) to 7 (Sunday again). When both day of month and day of week are restricted, cron runs the job if either matches, which surprises many people.

How do I edit and list crontabs?

Use crontab -e for your own jobs and add -u for another account. Never edit the spool files under /var/spool/cron directly:

crontab -e
crontab -l
sudo crontab -u www-data -l
sudo crontab -e

System-wide jobs can also live in /etc/cron.d/. Files there need an extra user field between the time and the command, must not contain dots in their name on Debian and Ubuntu, and should end with a newline:

echo "20 1 * * * deploy /home/deploy/bin/cleanup.sh" | sudo tee /etc/cron.d/cleanup
sudo chmod 644 /etc/cron.d/cleanup

Scripts dropped into /etc/cron.daily, cron.weekly or cron.monthly run at times the distribution picks; on Debian and Ubuntu their names must also avoid dots.

Why does my script work by hand but fail in cron?

cron runs commands with a tiny environment: a short PATH, /bin/sh as the shell and no variables from your .bashrc. Fix it by using absolute paths and setting what you need at the top of the crontab:

SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=""
15 3 * * * /home/deploy/bin/backup.sh >> /home/deploy/logs/backup.log 2>&1

The redirection at the end keeps every run in a log file. Without it, output goes to local mail, which on most servers is never read. A percent sign in a crontab command means a newline, so escape it as \%, for example in date +\%F. Test a script the way cron sees it with env -i /bin/sh -c /home/deploy/bin/backup.sh.

How do I stop runs from overlapping?

Wrap the command in flock, which takes a lock file and skips the run if the previous one is still busy:

*/5 * * * * flock -n /tmp/sync.lock /home/deploy/bin/sync.sh

With -n a second copy exits at once instead of waiting. This matters for jobs whose duration varies, such as uploads over a slow link or a database dump that grows every week. systemd timers avoid the problem by design, because a timer will not start a service that is still active.

How do I create a systemd timer?

Write a oneshot service that does the work, then a timer with the same base name that triggers it. First the service, /etc/systemd/system/backup.service:

[Unit]
Description=Nightly backup

[Service]
Type=oneshot
User=deploy
ExecStart=/home/deploy/bin/backup.sh
Nice=10
IOSchedulingClass=idle

Then the timer, /etc/systemd/system/backup.timer:

[Unit]
Description=Run backup every night

[Timer]
OnCalendar=*-*-* 03:15:00
RandomizedDelaySec=10min
Persistent=true

[Install]
WantedBy=timers.target

Enable the timer, not the service:

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

Persistent=true runs a missed job right after boot if the server was off at 03:15. RandomizedDelaySec spreads load when many timers share a minute. For a relative schedule use OnBootSec=5min together with OnUnitActiveSec=1h.

How do I test the schedule and see results?

Check the calendar expression, run the job once by hand and read the journal:

systemd-analyze calendar "Mon..Fri *-*-* 02:30:00" --iterations=3
sudo systemctl start backup.service
journalctl -u backup.service -n 30
systemctl list-timers --all

systemd-analyze calendar prints the next times the expression matches, which is the fastest way to catch a typo. For cron, the daemon logs each start: journalctl -u cron on Debian and Ubuntu or journalctl -u crond on Rocky and Alma. For one-off delayed tasks you do not need a file at all: sudo systemd-run --on-active=30min /home/deploy/bin/task.sh creates a transient timer.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-1scheduled scripts need very little1 vCPU, 1 GB RAM, 25 GB NVMe$3.60/mo$43.20 for 12 months
A1-2room for heavier nightly jobs1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months

Questions about Schedule jobs with cron and systemd timers

How do I run a job every 30 seconds?

cron cannot go below one minute. Use a systemd timer with OnUnitActiveSec=30s and AccuracySec=1s, or run a small loop inside a service.

Why did my job not run after a reboot?

cron does not catch up on runs missed while the server was off. A systemd timer with Persistent=true does. Also check that the cron or crond service is enabled.

How do I stop a cron job without deleting it?

Put a # at the start of the line in crontab -e. For a timer, run sudo systemctl disable --now name.timer.

Can I get an email when a job fails?

Set MAILTO in the crontab, or add OnFailure= to the service unit to trigger a notify unit. Mail needs a working relay on port 587; ask support by ticket before relying on direct delivery over port 25.

Where do I put a script that cron runs?

A folder such as /home/deploy/bin or /usr/local/bin, with a shebang line and execute permission (chmod +x). The first Bash script guide covers the basics.