How to Schedule Tasks on Raspberry Pi Using Cron
You can schedule tasks on Raspberry Pi using the
cron utility by editing the crontab file with crontab -e. Define the timing and command in the crontab using the syntax: minute hour day month weekday command.Syntax
The cron syntax for scheduling tasks uses five fields separated by spaces:
- minute: 0-59
- hour: 0-23
- day: 1-31
- month: 1-12
- weekday: 0-6 (Sunday=0)
- command: The script or command to run
Use * to mean 'every' for any field.
bash
* * * * * /path/to/command
Example
This example runs a Python script every day at 7:30 AM.
bash
30 7 * * * /usr/bin/python3 /home/pi/myscript.py
Common Pitfalls
Common mistakes include:
- Not using full paths for commands or scripts.
- Forgetting to make scripts executable with
chmod +x. - Not setting environment variables, causing commands to fail.
- Editing the wrong user's crontab.
Always test your commands manually before scheduling.
bash
# Wrong: relative path and no executable permission 30 7 * * * python3 myscript.py # Right: full path and executable script 30 7 * * * /usr/bin/python3 /home/pi/myscript.py
Quick Reference
| Field | Values | Description |
|---|---|---|
| minute | 0-59 | Minute of the hour |
| hour | 0-23 | Hour of the day |
| day | 1-31 | Day of the month |
| month | 1-12 | Month of the year |
| weekday | 0-6 | Day of the week (Sunday=0) |
| command | string | Command or script to run |
Key Takeaways
Use
crontab -e to edit scheduled tasks on Raspberry Pi.Always specify full paths for commands and scripts in cron jobs.
Test commands manually before scheduling them with cron.
Use
* to represent 'every' value in time fields.Remember to make scripts executable and set correct permissions.