On this page
- Should I use cron or a systemd timer?
- Is cron installed and running?
- How do I read the crontab time fields?
- How do I edit and list crontabs?
- Why does my script work by hand but fail in cron?
- How do I stop runs from overlapping?
- How do I create a systemd timer?
- How do I test the schedule and see results?
- Questions
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 crondcron 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.shThe 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 -eSystem-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/cleanupScripts 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>&1The 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.shWith -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=idleThen 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.targetEnable the timer, not the service:
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers backup.timerPersistent=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 --allsystemd-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.
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.
More in Server basics
- Create a sudo user
- Add a swap file
- Change the hostname
- Time zone and time sync
- File and directory permissions
- Choose a Linux distribution
- Upgrade Ubuntu to the next LTS
- Upgrade Debian to a new major release
- Rocky or AlmaLinux to a new major version
- Automatic security updates
- Run an app as a systemd service
- Environment variables on Linux
- Check disk space and find what fills it
- Inspect processes and memory
- Read the hardware and system report
- Reboot and shut down safely
- Customise the login message (MOTD)
- Remote desktop with Xfce and xrdp
- SELinux on Rocky Linux and AlmaLinux
- Keep sessions alive with tmux
- SFTP-only users in a chroot
- apt and dnf package management
- First-hour checklist for a new server
- Upgrade FreeBSD to a new release