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

| Field | Values | Notes |
|---|---|---|
| Minute | 0–59 | |
| Hour | 0–23 | 24-hour format, system local time |
| Day of month | 1–31 | |
| Month | 1–12 | Also jan, feb… |
| Day of week | 0–7 | 0 and 7 are Sunday; also mon, tue… |
In each field you can use:
*→ any value.,→ list:1,15means “the 1st and the 15th”.-→ range:1-5means “Monday to Friday”./→ step:*/10means “every 10”.
Example table
| Expression | When it runs |
|---|---|
*/5 * * * * | Every 5 minutes |
0 * * * * | At the start of every hour |
30 3 * * * | Every day at 03:30 |
0 8 * * 1-5 | Monday to Friday at 08:00 |
0 0 1 * * | On the 1st of each month at midnight |
15 4 * * 0 | Sundays at 04:15 |
0 9,21 * * * | At 09:00 and 21:00 |
@reboot | Once, at system startup |
@daily | Same as 0 0 * * * |
@weekly | Same 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.
- Open your crontab:
crontab -e
- The first time it asks for an editor. If unsure, nano is the simplest.
- 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
- Save and close (in nano: Ctrl+O, Enter, Ctrl+X). You will see
crontab: installing new crontab. No restart needed. - 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. Thersits right next to theeon 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.

Practical consequences:
- A command in
/usr/local/binor~/.local/binis not found. - A script that opens
datos.csvwithout 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 line | What it does |
|---|---|
>> /ruta/log 2>&1 | Appends output and errors to the log |
> /ruta/log 2>&1 | Same, but overwrites the log on each run |
> /dev/null 2>&1 | Discards 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:
| Need | Cron | systemd timer |
|---|---|---|
| Quick one-line syntax | Yes | No: two files |
| Catch up runs missed while the Pi was off | No | Yes, with Persistent=true |
| Wait for the network | Manual, with sleep | Yes, with After=network-online.target |
| Automatic journal logs | Only the launch | The whole script output |
| Avoid two overlapping runs | Manual | Yes, built in |
| Random delay to avoid spikes | No | Yes, with RandomizedDelaySec |

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
PATHor a relative path. Use absolute paths for everything. - The script is not executable. Grant it with
chmod +x script.shor 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/crontabwithout a user. Unlikecrontab -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-configunder Localisation Options. - The task depends on the network at boot and sometimes fails with
@reboot. Move it to systemd.



