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.

Comments

  1. Pingback: Auto running programs « Raspberry Pi Projects

  2. m16103

    9 months ago

    This works. After testing several I found this working script

  3. CheeWei

    9 months ago

    Why it don’t autorun when start-up?
    When I type ‘ /etc/rc.local’ at LXTerminal, it works.
    But it don’t auto start when I reboot it.
    Any solutions?

  4. kkyang

    7 years ago

    Have you solved your problem? I still cannot work, even type /etc/rc.local, it says the command cannot found, mine has input parameters, such as”
    sudo /home/pi/project/program inputPara

  5. olivier

    7 years ago

    do we have to call this script script_auto_run or we can give it any name (let say “mike” for example) ?

    1. Deidas

      6 years ago

      You can give any name

  6. Kaja

    7 years ago

    Has anyone solved that problem?

  7. abarna

    7 years ago

    I have written a program for color coding using python… now its working properly at start up. but i need to add patter matching code with existing code.. how can i edit and add my new codes? is it tough editing the start up running code?

  8. Kestutis Igaris

    6 years ago

    Works, but how do I stop the executable? In my case its video running in a loop and taking up the whole screen. How to stop it?

  9. Vennila

    6 years ago

    I am currently in directory /home/pi/IoT

    while executing I am getting error named import error: no module named imbiotf.application.
    ibmiotf.application is in my program. Can any one help me?

  10. Don Rich

    5 years ago

    Running 2016-5-10 Raspian on Raspberry Pi 3 and find that /etc/rc.local does not run on bootup.
    here is a copy of my rc.local file. Neither the IP address print routing nor my play_tcp are run on bootup.
    “#!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will “exit 0” on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.

    # Print the IP address

    sudo /usr/local/bin/play_tcp -a 192.168.0.115 &

    _IP=$(hostname -I) || true
    if [ “$_IP” ]; then
    printf “My IP address is %sn” “$_IP”
    fi

    exit 0

    Anyone know why it doesn’t execute? If I type /etc/rc.local it runs both parts, but it won’t run automatically on reboot.
    Don

  11. Ray Rodriguez

    5 years ago

    tip if you don’t already know know…
    export PATH=$PATH:/home/pi/bin

    this will add the created bin directory to the existing path for compilation

Comments

Your email address will not be published.