Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 37 ยท Automation and Troubleshooting

Scheduling with cron and systemd Timers

Schedule recurring jobs on Linux: cron expression syntax, crontab and /etc/cron.d, the cron environment pitfalls, anacron, systemd timer units, and logging and locking for reliable jobs.

Servers have chores: rotate logs, back up databases, renew certificates, clean temp files, send reports. An administrator who does these by hand will eventually forget. Linux has two mature schedulers: cron, the classic one, and systemd timers, which fit into the service manager you already use. This lesson teaches both, the traps that make "it works in my shell but not in cron" so common, and how to make scheduled jobs observable and safe.

The scheduler itself needs a real system. The commands are read-only; the Python stand-ins parse cron expressions and compute next run times so you can check your schedules before deploying them.

cron expressions

A crontab line has five time fields and then the command:

TEXT
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ minute (0-59)
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€ hour (0-23)
โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€ day of month (1-31)
โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€ month (1-12 or jan-dec)
โ”‚ โ”‚ โ”‚ โ”‚ โ”Œ day of week (0-7, 0 and 7 are Sunday, or sun-sat)
โ”‚ โ”‚ โ”‚ โ”‚ โ”‚
* * * * *  command
Field value Meaning
* every value
5 exactly 5
1,15 a list: 1 and 15
9-17 a range
*/10 a step: every 10th (0, 10, 20 ...)
10-50/10 a stepped range: 10, 20, 30, 40, 50

Examples:

TEXT
*/5 * * * *       /usr/local/bin/check-queue        # every 5 minutes
30 2 * * *        /usr/local/bin/backup.sh          # 02:30 every day
0 9-17 * * 1-5    /usr/local/bin/report             # on the hour 9-17, Monday-Friday
0 0 1 * *         /usr/local/bin/monthly-cleanup    # midnight on the 1st
15 4 * * sun      /usr/local/bin/weekly-vacuum
@reboot           /usr/local/bin/warm-cache         # shortcuts: @hourly @daily @weekly @monthly @yearly

A subtle rule: when both day-of-month and day-of-week are restricted (neither is *), cron runs the job when either matches, not both. 0 0 13 * 5 runs on every 13th and every Friday, not only on Friday the 13th.

Python Does this cron expression match?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Where crontabs live

  • Per-user: crontab -e (edit), crontab -l (list), crontab -r (remove all; there is no confirmation, so be careful). Lines have five time fields and a command, run as that user. Stored in /var/spool/cron/.
  • System-wide: /etc/crontab and files in /etc/cron.d/ have an extra user field after the time fields: 30 2 * * * root /usr/local/bin/backup.sh. Configuration management tools prefer /etc/cron.d/ drop-ins.
  • Directories /etc/cron.hourly, cron.daily, cron.weekly, cron.monthly: drop an executable script in (no dots in the file name on Debian, or run-parts ignores it).
  • Access control: /etc/cron.allow and /etc/cron.deny.

anacron runs missed daily/weekly/monthly jobs after a machine that was off comes back, which is why laptops and workstations use it for the cron.daily directories.

The cron environment and other traps

Cron starts jobs with a tiny environment: PATH=/usr/bin:/bin, SHELL=/bin/sh, no profile, home directory only. Many "works when I run it" failures come from that.

  1. Use absolute paths for commands and files, or set PATH= at the top of the crontab.
  2. % is special in a crontab command (it means newline). Escape it: date +\%F.
  3. Output becomes mail. Anything printed is mailed to the user (or silently dropped if there is no mail system). Redirect on purpose: >> /var/log/job.log 2>&1, or use logger.
  4. Overlapping runs. If a job takes longer than its interval, copies pile up. Use flock:
    TEXT
    */5 * * * * flock -n /run/lock/sync.lock /usr/local/bin/sync.sh
    
  5. Time zones and DST. cron uses the system time zone. A job at 02:30 may not run (or run twice) on a daylight-saving change day; keep servers on UTC.
  6. Silent failure. Add [email protected] at the top, and have jobs exit non-zero with a clear message. Better still, use a heartbeat service: the job pings a URL when it succeeds, and you are alerted if the ping does not arrive.

Check that cron ran your job in the logs: journalctl -u cron (Debian: cron, RHEL: crond), grep CRON /var/log/syslog.

systemd timers

A timer is a unit that starts a service. You write two small files: backup.service (what to run) and backup.timer (when).

TEXT
# /etc/systemd/system/backup.service
[Unit]
Description=Nightly backup

[Service]
Type=oneshot
User=backup
ExecStart=/usr/local/bin/backup.sh
Nice=10
IOSchedulingClass=idle
TEXT
# /etc/systemd/system/backup.timer
[Unit]
Description=Run backup nightly

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

[Install]
WantedBy=timers.target
Terminal
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers --all
systemctl status backup.service
journalctl -u backup.service -n 50
sudo systemctl start backup.service        # run it now, for testing
systemd-analyze calendar "Mon..Fri *-*-* 09..17:00:00"   # validate and preview a schedule

Timer advantages over cron: logs go straight into the journal per unit, resource limits and sandboxing come free (Nice, MemoryMax, ProtectSystem), Persistent=true catches up on runs missed while the machine was off, RandomizedDelaySec spreads load when many servers share a schedule, jobs cannot overlap (a service already running is not started again), and OnFailure= can alert. Besides OnCalendar there are monotonic timers: OnBootSec=5min, OnUnitActiveSec=1h ("one hour after the last run").

OnCalendar syntax is DayOfWeek Year-Month-Day Hour:Minute:Second, with shortcuts hourly, daily, weekly, monthly; *-*-* 02:30:00 is daily at 02:30, Mon,Fri *-*-* 09:00 is Monday and Friday at nine, and *:0/15 is every 15 minutes.

Python Translate cron to OnCalendar

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Which should you use?

Use systemd timers for new services on systemd machines where you want logging, sandboxing and dependency handling. Use cron when you need simplicity, portability to non-systemd systems, or per-user jobs without root. For one-time jobs use at (echo "reboot" | at 03:00) or systemd-run --on-active=30m /path/to/script.

Reliable scheduled jobs

Absolute paths Cron has a tiny PATH.
flock No overlapping runs.
Log it Journal or logger.
Alert on silence Heartbeat on success.
1 / 2

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the crontab line 0 9-17 * * 1-5 command do?

    Minute 0, hours 9 through 17, any day of the month and month, weekdays 1 to 5.
  2. 2. A cron script works in your terminal but fails under cron. What is the most likely cause?

    Use absolute paths and set needed variables explicitly.
  3. 3. What does Persistent=true do in a systemd timer?

    Persistent timers remember the last trigger time on disk and catch up.
  4. 4. How do you stop a slow job from overlapping its own next run in cron?

    flock -n exits immediately if the previous run still holds the lock.

Summary

  • A cron line has five time fields (minute, hour, day of month, month, day of week); day-of-month and day-of-week combine with OR when both are restricted.
  • Crontabs live per user (crontab -e), in /etc/cron.d/ (with a user field) and in the cron.daily style directories.
  • Cron's small environment, % characters, mail output and overlapping runs cause most problems; use absolute paths, redirects and flock.
  • systemd timers pair a .timer with a .service, log to the journal, support Persistent=, randomised delay and sandboxing.
  • Validate schedules (systemd-analyze calendar) and alert when a job silently stops.

Next: Shell Scripts for Administrators