cron & systemd
Servers need things to happen without anyone typing: nightly backups, hourly reports, a web app that starts at boot and restarts when it crashes. cron runs commands on a schedule; systemd starts, supervises and logs services (and can schedule jobs too, with timers).
Note: Both are system services that need a running daemon (and often root), so the commands are shown with sample output. The runnable examples work with the configuration formats, which is where most mistakes happen.
crontab: the schedule format
Each line of a crontab has five time fields followed by the command:
┌───────── minute (0-59)
│ ┌─────── hour (0-23)
│ │ ┌───── day of month (1-31)
│ │ │ ┌─── month (1-12)
│ │ │ │ ┌─ day of week (0-7, 0 and 7 = Sunday)
│ │ │ │ │
30 2 * * * /home/ada/bin/backup.sh| Syntax | Meaning | Example |
|---|---|---|
* |
Every value | * * * * * every minute |
n |
Exactly n | 30 2 * * * at 02:30 daily |
a,b |
A list | 0 9,17 * * * at 09:00 and 17:00 |
a-b |
A range | 0 9 * * 1-5 weekdays at 09:00 |
*/n |
Every n-th | */15 * * * * every 15 minutes |
@daily, @hourly, @reboot |
Shortcuts | @reboot /opt/app/start.sh |
Managing your crontab
crontab -l # list your jobs
crontab -e # edit them in $EDITOR
crontab -r # remove ALL your jobs (careful!)
sudo crontab -u www-data -l # another user's jobs
ls /etc/cron.d /etc/cron.daily # system-wide jobsA real crontab with good habits:
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
MAILTO=ops@example.com
# m h dom mon dow command
30 2 * * * /home/ada/bin/backup.sh >> /home/ada/logs/backup.log 2>&1
*/10 * * * 1-5 /home/ada/bin/check-site.sh
0 0 1 * * /home/ada/bin/monthly-report.shTip: cron runs jobs with a minimal environment: a short
PATH, no aliases, no.bashrc, and the home directory as working directory. Use absolute paths, setPATHat the top, and redirect output to a log file, otherwise errors vanish (or are mailed to a mailbox nobody reads).
Reading a crontab with a script
The format is plain text, so you can check it with awk. This one explains each job of a sample crontab file:
cat > jobs.cron <<'CRON'
# m h dom mon dow command
30 2 * * * /opt/bin/backup.sh
*/15 * * * * /opt/bin/check-site.sh
0 9 * * 1-5 /opt/bin/standup-reminder.sh
@reboot /opt/bin/start-app.sh
CRON
awk '/^#/ || NF == 0 { next }
$1 ~ /^@/ { print $1 ": " $2; next }
{ cmd = $6; for (i = 7; i <= NF; i++) cmd = cmd " " $i
printf "min=%-5s hour=%-3s dom=%s mon=%s dow=%-4s %s\n", $1, $2, $3, $4, $5, cmd }' jobs.cronmin=30 hour=2 dom=* mon=* dow=* /opt/bin/backup.sh
min=*/15 hour=* dom=* mon=* dow=* /opt/bin/check-site.sh
min=0 hour=9 dom=* mon=* dow=1-5 /opt/bin/standup-reminder.sh
@reboot: /opt/bin/start-app.sh
Does a field match? A tiny cron matcher
To really understand the syntax, here is a Bash function that checks whether one cron field matches a value. It supports *, numbers, lists, ranges and steps:
field_matches() { # field_matches FIELD VALUE
local field=$1 value=$2 part
IFS=, read -r -a parts <<< "$field"
for part in "${parts[@]}"; do
local step=1
[[ $part == */* ]] && { step=${part#*/}; part=${part%/*}; }
local lo hi
if [[ $part == "*" ]]; then lo=0; hi=59
elif [[ $part == *-* ]]; then lo=${part%-*}; hi=${part#*-}
else lo=$part; hi=$part
fi
if (( value >= lo && value <= hi && (value - lo) % step == 0 )); then return 0; fi
done
return 1
}
# minute 45, hour 9, Wednesday (3)
for test in "*/15 45" "0,30 45" "1-5 3" "6,0 3" "9-17/2 9"; do
read -r field value <<< "$test"
if field_matches "$field" "$value"; then r=yes; else r=no; fi
printf '%-8s matches %-3s %s\n' "$field" "$value" "$r"
done*/15 matches 45 yes
0,30 matches 45 no
1-5 matches 3 yes
6,0 matches 3 no
9-17/2 matches 9 yes
A job runs when all five fields match the current time (with one quirk: if both day-of-month and day-of-week are restricted, matching either is enough).
systemd services
On almost every modern distribution, systemd is PID 1. It starts services described by unit files and keeps them running:
sudo systemctl start nginx # start now
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx # re-read config without dropping connections
sudo systemctl enable --now nginx # start at boot, and now
systemctl status nginx # state, PID, last log lines
systemctl list-units --type=service --state=failed
journalctl -u nginx --since today # logs of one service
journalctl -f # follow all logs (like tail -f)$ systemctl status shop
● shop.service - Shop web application
Loaded: loaded (/etc/systemd/system/shop.service; enabled; preset: enabled)
Active: active (running) since Sun 2026-03-15 09:12:01 UTC; 2h 3min ago
Main PID: 1342 (server)A unit file for your own app, saved as /etc/systemd/system/shop.service:
cat > shop.service <<'UNIT'
[Unit]
Description=Shop web application
After=network-online.target
[Service]
User=shop
WorkingDirectory=/opt/shop
Environment=PORT=8080
ExecStart=/opt/shop/bin/server
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
UNIT
for key in User ExecStart Restart WantedBy; do
printf '%-10s = %s\n' "$key" "$(grep -m1 "^$key=" shop.service | cut -d= -f2-)"
done
grep -c '^\[' shop.serviceUser = shop
ExecStart = /opt/shop/bin/server
Restart = on-failure
WantedBy = multi-user.target
3
After creating or changing a unit file, run sudo systemctl daemon-reload, then enable --now. Restart=on-failure is what makes systemd better than a cron @reboot line: the service is restarted if it crashes, and its output goes to the journal automatically.
systemd timers: cron's successor
A timer unit triggers a service on a schedule. OnCalendar=*-*-* 02:30:00 means "daily at 02:30"; Persistent=true runs a missed job after the machine was off:
# backup.timer
[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true
[Install]
WantedBy=timers.target$ systemctl list-timers
NEXT LEFT LAST PASSED UNIT ACTIVATES
Mon 2026-03-16 02:30:00 UTC 15h left Sun 2026-03-15 02:30:04 UTC 8h ago backup.timer backup.serviceTest yourself
What does the crontab line 0 */6 * * * job.sh do?
- Runs every 6 minutes
- Runs at minute 0 every 6 hours
- Runs on the 6th of each month
- Runs 6 times a day at random
Why do commands that work in your terminal sometimes fail in cron?
- cron is slower
- cron uses a minimal environment: short PATH, no .bashrc
- cron needs root
- cron can't run scripts
Which command shows the recent logs of the nginx service on a systemd machine?
- systemctl logs nginx
- journalctl -u nginx
- cat /var/log/systemd
- crontab -l nginx