Skip to content
GEEKnodes

Raspberry Pi

Cron on Raspberry Pi: Schedule Automatic Tasks With crontab

Schedule tasks on a Raspberry Pi with cron: the 5-field syntax, crontab -e, @reboot, absolute paths, journalctl logs and when to use a systemd timer.

Nightly backups, a script that reads a sensor every 5 minutes or a weekly reboot: all of that is scheduled with cron. The problem is that a script that works perfectly in the terminal often fails silently when cron runs it. Here you will see the syntax, why that happens and how to check whether the task really ran.

In short

  • Each crontab line has 5 time fields (minute, hour, day of month, month, day of week) followed by the command.
  • Cron starts commands with a minimal environment: always use absolute paths and redirect output to a log.
  • On Raspberry Pi OS Bookworm, cron logs are in the journal: query them with journalctl -u cron.

Everything below applies to Raspberry Pi OS Bookworm, both Lite and desktop. Cron comes installed and enabled.

How to read a crontab line

A task is a line in this format:

minute  hour  day-of-month  month  day-of-week  command
Anatomy of a crontab line: five time fields (minute, hour, day of month, month and day of week) followed by the command
Five time fields, then the command to run.
FieldValuesNotes
Minute0–59
Hour0–2324-hour format, system local time
Day of month1–31
Month1–12Also jan, feb…
Day of week0–70 and 7 are Sunday; also mon, tue…

In each field you can use:

  • * → any value.
  • , → list: 1,15 means “the 1st and the 15th”.
  • - → range: 1-5 means “Monday to Friday”.
  • / → step: */10 means “every 10”.

Example table

ExpressionWhen it runs
*/5 * * * *Every 5 minutes
0 * * * *At the start of every hour
30 3 * * *Every day at 03:30
0 8 * * 1-5Monday to Friday at 08:00
0 0 1 * *On the 1st of each month at midnight
15 4 * * 0Sundays at 04:15
0 9,21 * * *At 09:00 and 21:00
@rebootOnce, at system startup
@dailySame as 0 0 * * *
@weeklySame as 0 0 * * 0

A confusing detail: if you restrict both day of month and day of week, cron runs the task when either matches, not both. 0 12 13 * 5 runs every 13th and every Friday, not only on Friday the 13th.

Edit the crontab step by step

Each user has their own crontab. Tasks run with that user’s permissions.

  1. Open your crontab:
crontab -e
  1. The first time it asks for an editor. If unsure, nano is the simplest.
  2. Add the task at the end of the file, for example:
*/5 * * * * /usr/bin/python3 /home/pi/scripts/sensor.py >> /home/pi/logs/sensor.log 2>&1
  1. Save and close (in nano: Ctrl+O, Enter, Ctrl+X). You will see crontab: installing new crontab. No restart needed.
  2. Check what is installed:
crontab -l

For tasks that need administrator rights, use root’s crontab with sudo crontab -e. It is better than putting sudo inside your own crontab, because nobody is there to type the password.

Beware of crontab -r: it deletes the whole crontab without asking. The r sits right next to the e on the keyboard.

The minimal environment: why your script fails in cron

When you open a terminal, your shell loads .bashrc, the full PATH, your variables and the current directory. Cron loads none of that. It runs commands with /bin/sh, a reduced PATH (usually just /usr/bin:/bin) and your home as the working directory.

Comparison between the full terminal environment and cron's minimal environment, with PATH reduced to /usr/bin and /bin
Cron does not load your .bashrc: use absolute paths for everything.

Practical consequences:

  • A command in /usr/local/bin or ~/.local/bin is not found.
  • A script that opens datos.csv without a path looks for it in your home folder, not next to the script.
  • If you use a Python virtual environment, cron does not activate it.

The solution is to be explicit:

*/10 * * * * /home/pi/env/bin/python /home/pi/scripts/lectura.py >> /home/pi/logs/lectura.log 2>&1

Calling the python inside the virtual environment directly is the same as activating it. If you need variables, declare them at the top of the crontab:

PATH=/usr/local/bin:/usr/bin:/bin
SHELL=/bin/bash

To find a program’s path, use which:

which rsync

The % symbol is special

In a crontab, % means newline. A command like date +%Y-%m-%d breaks. Escape it with a backslash: date +\%Y-\%m-\%d. A cleaner option is to put the logic in a script and call only the script.

Redirect output to a log

By default cron tries to email what the command prints. Raspberry Pi OS has no mail server, so that output is lost and the journal shows a warning like “No MTA installed, discarding output”. If the script fails, you never find out.

So end each line with a redirect:

End of lineWhat it does
>> /ruta/log 2>&1Appends output and errors to the log
> /ruta/log 2>&1Same, but overwrites the log on each run
> /dev/null 2>&1Discards everything (only if you know it works)

The log folder must exist beforehand; cron does not create it. A log with >> grows forever: if the task runs often, empty it now and then or use logrotate.

@reboot: tasks at startup

@reboot runs the command once when the cron service starts:

@reboot sleep 30 && /home/pi/scripts/inicio.sh >> /home/pi/logs/inicio.log 2>&1

The sleep 30 is not decorative. Cron starts early, and at that moment the network or a USB drive may not be ready yet. If your script depends on them, wait, or better, use a systemd service with dependencies (below).

Check whether a task ran

Bookworm has no /var/log/syslog by default: logs go to the systemd journal. To see cron activity:

journalctl -u cron --since today

Each run appears as a line with CMD and the command. To follow it live while waiting for the task:

journalctl -u cron -f

If the CMD line appears but the result is wrong, the problem is in the script: check your log. If it does not appear, check the syntax with crontab -l and that the system time is right with timedatectl. Most Raspberry Pi models have no battery clock and take the time from the network at boot.

When to use a systemd timer

Cron is perfect for simple tasks. A systemd timer pays off when you need more control:

NeedCronsystemd timer
Quick one-line syntaxYesNo: two files
Catch up runs missed while the Pi was offNoYes, with Persistent=true
Wait for the networkManual, with sleepYes, with After=network-online.target
Automatic journal logsOnly the launchThe whole script output
Avoid two overlapping runsManualYes, built in
Random delay to avoid spikesNoYes, with RandomizedDelaySec
A systemd timer made of two files: copia.timer starts copia.service, which runs the script and writes to the journal
Two files: the timer decides when and the service decides what.

A minimal example. First the service, in /etc/systemd/system/copia.service:

[Unit]
Description=Daily data backup

[Service]
Type=oneshot
User=pi
ExecStart=/home/pi/scripts/copia.sh

Then the timer, in /etc/systemd/system/copia.timer:

[Unit]
Description=Runs the backup every day at 03:30

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

[Install]
WantedBy=timers.target

And enable it with:

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

The script output goes to journalctl -u copia.service, no redirects needed. Replace pi with your user: since Bullseye, Raspberry Pi OS no longer creates the pi user by default.

Common mistakes

  • The script works by hand but not in cron. Almost always the PATH or a relative path. Use absolute paths for everything.
  • The script is not executable. Grant it with chmod +x script.sh or call it with its interpreter (/bin/bash script.sh).
  • Missing #! line at the top of the script. Without it, cron does not know which interpreter to use.
  • Using unescaped % in the command: the line is cut and fails.
  • Editing /etc/crontab without a user. Unlike crontab -e, that file has a sixth field with the user before the command.
  • Wrong time. If the time zone is not yours, tasks run offset. Set it in sudo raspi-config under Localisation Options.
  • The task depends on the network at boot and sometimes fails with @reboot. Move it to systemd.

Accessibility preferences

Quick profiles

Text and reading

Text size
Default
Line height
Default
Letter spacing
Default
Word spacing
Default

Colour

Motion and orientation

Cursor