Starting programs automatically is extremely useful for Raspberry Pi projects. In this tutorial, we are going to start programs automatically on boot and at regular time intervals on a Raspberry Pi using Cron.
What is Cron?
Cron is the default task scheduler utility on Linux-based operating systems like the Raspberry Pi OS. It’s the perfect tool for this application as it can start programs and shell scripts on boot or at regular intervals. Cron is widely used in automating recurring tasks such as sending sensor data reports, updating the weather daily, watering the plants, etc.
How to Run a Program on Startup
1. First, open the crontab using the command below.
sudo crontab -e
Crontab lets you view and edit the cron table file. The cron table file is a list of scheduled tasks for a particular user on the device. Even the root user has it.
2. Next, choose a text editor. In my experience, I was asked for my preferred text editor when I used the command line using the ALT + F1 shortcut. I tried it again using the desktop terminal, and it didn’t ask me. It just opened the file using nano. Using a different text editor doesn’t make a difference so just choose the one you’re comfortable with.
3. Next, add the program you want to run on boot at the end of the file.
@reboot python3 /home/pi/filename.py
To run the program on boot, the line must have @reboot
at the beginning. After that, you can add whatever it is that you need to. For our example, we run the python3 program to launch the python3 interpreter, followed by the script’s path.
4. Lastly, save and exit.
Running a program on startup requires you to have an
execute permission with the application and file. You can check using ls -l
. Otherwise, to add the permission, enter sudo chmod a+x FILENAME
.
How to Run a Program at Regular Intervals
1. Open crontab.
sudo crontab -e
2. Choose a text editor.
3. Add a scheduled task.
In order to do that, we must get familiarized with the format of a cron table entry. Notice the last line:
# m h dom mon dow command
The layout for a cron entry has six components: minute (m), hour (h), day of the month (dom), the month of the year (mon), day of the week (dow), and the command to be executed (command).
# * * * * * command to execute
# ┬ ┬ ┬ ┬ ┬
# │ │ │ │ │
# │ │ │ │ │
# │ │ │ │ └───── day of week (0 - 7) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
# │ │ │ └────────── month (1 - 12)
# │ │ └─────────────── day of month (1 - 31)
# │ └──────────────────── hour (0 - 23)
# └───────────────────────── min (0 - 59)
For instance, suppose you want to run a program named filename.py at 3:00 pm every day. The entry would look like this:
15 0 * * * python3 /home/pi/filename.py
4. Save and exit.
5. To view your currently scheduled tasks, enter the command below:
crontab -l
Thanks for reading and be sure to leave a comment below if you have questions about anything!
As shown it will be 15 minutes after midnight each day, For every 1500 hrs it should read 0 15 * * * python3 /home/pi/filename.py
Thanks Mike, I was confused by that. it didn’t look right to me. Thanks for confirming the error.
How about writing about starting, running and stopping a program on a Raspberry Pi (or even an Arduino) by using a hardware real time clock (RTC) for those applications that require explicit and accurate timekeeping to fractions of a second and UTC?
As shown, the cron is added using ‘sudo crontab -e’. Later in the page, it is verified using ‘crontab -l’. I don’t think this will work. If you want to see the sudo’s crontab, you should run ‘sudo crontab -l’.
Hi thank you for your info, however, I used this method (crontab) and for the day of week is actually (0 = Monday)