Execute at RPI boot or every minute
When setting up your Raspberry Pi (rpi) you sometimes need a command to be executed at boot or run it every n minutes continously (again, again and again). For example a service that should run be running in the background or piece of code written in Python, Node or NET Core that does every minute a simple task. In this blog i will explain both. The method I have choosen for those tasks is one of many possible methods. For this example I make use of some Python code but it can be any command you like.
We need a Raspberry Pi with Raspberry Pi OS (previously called Raspbian). I'm pretty sure it will work on other linux distribution. The only downside is that sometimes of the path's are different.
The command i want to run
Create (or have) Python code saved in
/home/pi/my_code.py. You can name whatever you wan't. I'll use the following command to create a simple piece of Python code that prints 'Hello Raspberry Pi.'.
echo "print('Hello Raspberry Pi.')" > /home/pi/my_code.py
To run the Python code I use the command below.
To run it in the background, add a
& symbol at the end of the command. What will happen is that after starting the command it immediately returns to the command line. The command will keep running in the background until it's finished.
python /home/pi/my_code.py &
Run at boot
Now let's try to earlier shown command at boot. For this we need to add the command to the
/etc/rc.local file. Execute the command below to open the GNU nano text editor to edit the rc.local file.
sudo nano /etc/rc.local
I add my command above the
exit 0 on the end of the file. My rc.local file looks like this. Take note of the
& symbol after the command. This will prevent the rc.local script of waiting until my code is finished.
python /home/pi/my_code.py & exit 0
Now save the file by pressing
CTRL + X. Nano will ask if you want to save the file. Press
Y (Yes) to save and exit, or press
N (No) to discard your changes. That's all, reboot the RPI and the python code will run and every time you (re)boot the system.
Run on login
The same thing as above can be done to run a script at login. It has almost the same steps to make it work.
To do this we need to add our command to the
/home/pi/.bashrc file. Open the GNU nano text editor to edit the .bashrc file with the command below.
sudo nano /home/pi/.bashrc
At the end of the file add our command.
There's no 'exit 0' in this file like in the rc.local.
python /home/pi/my_code.py &
Now save the file and logout and login again. The python code will run, every time you login.
Run every x minutes/hours or just weekly
Now we've learned how to run a command at boot and login you may also want to know how to run a command each minute or maybe every hour or only once a day. To do this we need to schedule a cron job by adding a line to the crontab file. This file is a configuration file for running cron jobs.
Run the command below to open a text editor for configuring the crontab file. This will probably open Nano.
sudo crontab -e
In this example I want my command to run every 5 minutes. Do this by adding the following line (cron job) to the file crontab file.
*/5 * * * * python /home/pi/my_code.py
Now save the file and exit. The command wil now run every 5 minutes.
Cron job configuration explained
I think the cron job configuration needs some explanation. The explanation below is a very short one. There is much more possible with cron jobs.
* (star) means 'always' and a
/ (slash) means 'interval'. The number
5 means 5 minutes. With cron job line above i tell the system to run my Python script always every 5 minutes
*/5 for every hour
* of the day and every month
* each day of the week
Each position (
* in the above example) represents a specific moment in time. Below I explain each position. The characters A B C D E can be ignored, those are only added for understanding.
* * * * * python /home/pi/my_code.py A B C D E
A is Minute
B is Hour
C is Day of the month
D is Month
E is Day of the week
7 (Starting at
0 sunday and ending at
* can also be replaced by another symbol. Each symbol has a different meaning. Symbols can be combined just like our example
, List of moments, for example
0,15,30,45 * * * * will run every 15th minute in an hour.
- Range, for example
* * * * 5-6 will run only on the weekend days.
/ Interval, for example
* * */15 * * will run once every 15 days.
To save you time; don't forget the
& while configuring a run at boot script.