Auto running a script

Create the script

Create a folder to store the script in


mkdir ./bin
cd ./bin

Create the script using the nano text editor


sudo nano script_auto_run

In the nano editor, type this script:


#!/bin/bash
# Script to start our application
echo "Doing autorun script..."
sudo /home/pi/projects/my_project.a &
 

Replace "sudo /home/pi/projects/my_project.a &" with the commands you want carried out.  The "&" means do the command in the background.

Note that when executing a command without logging is as a user you can't depend on any path or environment variables so you must provide full paths to everything.

Save it by pressing Ctrl+X, " Y", ENTER

This script needs to be made executable by typing this :


sudo chmod 755 script_auto_run

 

You can test the script works by typing


/home/pi/bin/script_auto_run

 

Setting it to be run

To launch the script at start-up edit the “rc.local” file needs to be edited.


sudo nano /etc/rc.local

 

Add the following line:


/home/pi/bin/script_auto_run

Save it by pressing Ctrl+X, " Y", ENTER

Re-boot your RPi and it will run.

Solving Issues

rc.local runs as root user which might cause problems for some actions you perform in scripts. You may also want a slight delay before a script is executed.  There's solutions to both of these issues here.

 

 

 





USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.

Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.