Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 37 ยท Deployment and Automation

Scheduling Jobs with cron and systemd Timers

Run tasks on a schedule on your Pi: cron syntax, crontab pitfalls with paths and environment, @reboot, systemd timers, logging, and a runnable cron expression matcher.

Some jobs should not run all the time: back up the database at 3 a.m., download a forecast every hour, restart a flaky service on Sunday. A Pi has two schedulers for this. cron is the classic, compact and everywhere. systemd timers are newer, integrate with logging and handle missed runs. Knowing both lets you pick the right one.

Scheduling needs a running system, so crontabs and unit files here are read-only. The runnable blocks decode and evaluate cron expressions in Python, which is the best way to build confidence in the five-field syntax.

cron basics

Each user has a crontab, a list of lines "when" and "what". Edit yours with crontab -e and list it with crontab -l. A line has five time fields followed by the command:

TEXT
# minute hour day-of-month month day-of-week  command
*/10 * * * *   /usr/bin/python3 /home/ana/weather/logger.py
30 3 * * *     /home/ana/bin/backup.sh
0 8 * * 1-5    /home/ana/bin/morning-report.sh
@reboot        /home/ana/bin/start-camera.sh
Field Range Notes
minute 0-59
hour 0-23
day of month 1-31
month 1-12
day of week 0-7 0 and 7 are Sunday

Inside a field: * is every value, 5 one value, 1,15 a list, 1-5 a range, */10 every tenth. The line */10 * * * * runs every ten minutes; 0 8 * * 1-5 runs at 08:00 Monday to Friday. Special strings: @reboot, @hourly, @daily, @weekly.

If both day-of-month and day-of-week are restricted (not *), cron runs when either matches, not both. That surprises almost everyone.

A cron matcher in Python

The function below parses one field and then tests whether a given time matches a full expression. Read it, then try the experiments.

Python Parse and match cron expressions

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.

Pitfalls that make cron jobs fail silently

Cron runs commands with a minimal environment: a short PATH, no login settings, the home directory as the working directory, and no terminal. The usual fixes:

  • Use absolute paths for the program and the files: /usr/bin/python3 /home/ana/weather/logger.py.
  • cd into the project or use full paths inside the script.
  • Use the virtual environment's interpreter: /home/ana/weather/venv/bin/python.
  • Redirect output, or you will never see errors: >> /home/ana/weather/cron.log 2>&1.
  • Percent signs are special in crontab lines; escape them (\%) in date +%F.
  • Jobs for GPIO should run as your user, not sudo crontab -e.

Mail from cron goes to a local mailbox nobody reads, so set MAILTO="" or redirect output yourself. Check whether cron even ran with journalctl -u cron, or grep CRON /var/log/syslog on systems that keep that file.

A wrapper that always logs, with a timestamp, saves hours of debugging:

Terminal
#!/bin/bash
echo "$(date -Is) start" >> /home/ana/cron.log
/usr/bin/python3 /home/ana/weather/logger.py >> /home/ana/cron.log 2>&1
echo "$(date -Is) exit $?" >> /home/ana/cron.log

Preventing overlapping runs

If a job takes longer than its interval, two copies run at once and fight over the same files. Use flock:

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

-n makes the second copy give up at once. The Python equivalent below shows the idea with a lock set; this stand-in uses a simple dictionary rather than a file lock.

Python Stand-in: skipping a run while the previous one is active

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.

systemd timers

A timer is a pair of units: backup.service (what to run, type oneshot) and backup.timer (when).

INI
# /etc/systemd/system/backup.timer
[Unit]
Description=Nightly backup

[Timer]
OnCalendar=*-*-* 03:30:00
Persistent=true
RandomizedDelaySec=300

[Install]
WantedBy=timers.target
Terminal
sudo systemctl daemon-reload
sudo systemctl enable --now backup.timer
systemctl list-timers                      # next and last run
sudo systemctl start backup.service        # run it now
journalctl -u backup.service

Advantages over cron: Persistent=true runs a missed job at the next boot (important for a Pi that is sometimes off), logs go to the journal automatically, you can limit resources, and OnBootSec=5min or OnUnitActiveSec=1h create relative schedules. Check an expression with systemd-analyze calendar "Mon *-*-* 03:30".

Which to use?

  • Quick personal job: cron.
  • Needs logs, retries, resource limits or catch-up after downtime: a timer.
  • Needs to run continuously: neither; use a service.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When does 0 8 * * 1-5 run?

    Minute 0, hour 8, any day or month, and days of week 1 to 5 (Monday to Friday).
  2. 2. Why does a script work in your terminal but not from cron?

    Use absolute paths and redirect output to a log.
  3. 3. What does flock -n do in a crontab line?

    -n fails immediately when the lock is held, preventing overlap.
  4. 4. Which timer setting runs a missed job after the Pi was off at the scheduled time?

    Persistent=true stores the last run time and catches up at next start.

Summary

  • A crontab line has minute, hour, day of month, month and day of week, then the command.
  • *, lists, ranges and steps combine; restricting both day fields uses OR.
  • Always use absolute paths and redirect output; cron's environment is minimal.
  • flock prevents overlapping runs.
  • systemd timers add logging, catch-up (Persistent=true) and relative schedules.

Next: Backups and Taking Care of the SD Card