Auto Run for your Raspberry Pi - how to have your Rpi boot up in the desired configuration.
Updated: Jun 24
The Raspberry Pi (Rpi) is a great single board computer (SBC) that can be used in many projects and comes in different configurations. I personally like how easy it is to set up as simple web server, to read sensors, to send emails, tweets and other notifications, leverage higher level languages such as Python and Linux, and the abundance of software available. I still use simpler, smaller and cheaper microcontrollers for more routine functions though.
When prototyping with the Rpi, programs are usually run from the development environment or from the command line. But what do you do when you want the Rpi to boot up with programs running? A few easy steps will make your Rpi fire up and operate the way you want. No doubt that there are a number of ways to do this but this is what I have developed over a series of projects including the Big Font Clock.
The basic steps are to 1) ensure that Rpi logs in on its own, 2) write a bash script with the configuration and program/script running instructions, 3) set permissions to allow the bash script to run and 4) modify the etc/profile file to run the bash script during boot up.
1) Autologin: During configuration of your Rpi be sure to enable autologin. To get to the configuration tool use the terminal to enter the command:
2) Make Directory for the auto run bash script: In the terminal, be sure to be in the home directory ~/ so we can make a new directory ./bin for the auto run script. Then we will go to that directory and build the script. In the script we will print to the screen, using echo, that an auto run script is running. We will then set up a specific IP address (optional) and then run a few python scripts. Be sure you know the paths to the directories that the python scripts are in. We will use ampersand (&) so that scripts run in the background and other process may run as well. The example below runs two python scripts, you may use this to run as many scripts or other programs as you wish.
cd ~ #if you are not in the home directory mkdir ./bin #makes a new directory cd ./bin #go to the new directory sudo nano ScriptAutoRun #build the auto run script
Now we will use nano to write the script. The #comments below are an explanation of what each step does. Do not add them verbatim, add your own comments so you can come back to the script and remember what you were doing.
#! /bin/bash echo "Performing auto run script" #tell what you are doing sudo ifconfig eth0 192.168.1.456 #setup specific IP address (option) cd /home/pi/file1directory #go to the directory for the first #script that you want to run sudo python3 file1.py & #runs the first python script sleep 1 #optional to allow script to start cd /home/pi/file2directory #go to the directory for the second #script that you want to run sudo python3 file2.py & #runs the second python script sleep 1 #optional to allow script to start echo "ip address" #if you want to show IP address sudo ifconfig #if you want to show IP address echo "All done!" #the script has completed
Now save the script and exit nano, you should see that ScriptAutoRun is in ./bin
3) Set Permissions: Still using the terminal, be sure you are in the ~ /bin directory so we can set the permissions for the script to run
sudo chmod u+x ScriptAutoRun sudo chmod g+x ScriptAutoRun sudo chmod o+x ScriptAutoRun
Now go back to the home directory
4) Run Auto Run Script at Boot: Now we have to add the auto run script to ~/etc/profile so it runs when the Rpi boots up
sudo nano /etc/profile
Go all the way to the bottom of the file and add the following line
Save the changes and exit nano. Test your work with a restart
sudo shutdown -r now
I am sure there are variations to the above code and probably better ways to do auto run scripts. Please leave constructive comments if you have any other ideas or approaches!
Note that bash does not always interpret underscores "_" correctly, so try to avoid them. If you use them, you may get error messages that the file or directory does not exist when the auto run script tries to run. I have found that substituting "\ " (forward slash followed by a space) in the bash script will work as a substitute if you have underscores in your file or directory names.
GL de AI6XG