Auto running applications in the GUI

See here

Auto running terminal applications (non GUI)

First ensure your program is executable by finding it in the file manager.  Right click on the file and select properties.  Select the permissions tab, check the ‘Make the file executable’ box and press OK.  Or from the command line use:


sudo chmod +x /home/pi/projects/my_project.a

Or using a different tool set its chmod to 755 (rwxr-xr-x).  It doesn't matter if the user is root.

Doesn't work?

We've found that when copying a new file to the rpi using WinSCP, changing its properties to 755 and verifying all is OK that if we kill the power and power up again the executable doesn't run.  However if we use sudo reboot at the command line it works as it should.  It seems there is some sort of caching action going on so after doing this use sudo reboot the first time rather than cycling the power!

You can setup the auto run using a script (see here), or you can do it directly by editing the rc.local file:


sudo nano /etc/rc.local

 

After the initial comments (lines beginning with '#') add the following lines:


# Auto run our application
sudo /home/pi/projects/my_project.a &

"sudo" assumes you want your application run with root user privileges (remove if not) and the "&" says do it in the background.

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

Re-boot your RPi and it will run.

To kill the program

If you need to kill your program running in the background you can use this


sudo killall my_project.a
If the program has auto run but is in a loop and you can't kill it!

Often the keyboard interface to the RPi is still working even though you may not see what you type due to your program writing to the screen in the background.  Try typing your username and then password as you normally would at login and then the "sudo killall" above and it may well kill it

Running multiple things and also programs as a non root user

You can use brackets around multiple commands each separated by a ';' followed by the '&' to run the set of commands all in the background.

If you use the '&' you can also run multiple lines as each is run in the background.

Finally, by default rc.local runs as the root user.  You can change to a different user using su – USERNAME -c before the command and surrounding it with quotes.

In the example below the following occurs:

After a 2 second pause my_project.a us run in the background.

After a 5 second delay VLC is started to stream the raspberry pi video camera.  VLC won't run as the root user so su -c is used to make it run as the user "pi"


#Auto run our application
(sleep 2;sudo /home/pi/projects/my_project/my_project.a)&
#Auto run VLC video streaming
(sleep 5;su - pi -c "raspivid -o - -t 0 -n -w 640 -h 480 -fps 15 | cvlc -vvv stream:///dev/stdin --sout '#rtp{sdp=rtsp://:8554/}' :demux=h264")&

 

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. John Ktejik

    8 years ago

    sudo killall does not work. Do you have a better idea to kill a program on a loop?

  2. dave

    8 years ago

    This starts them at logon…how can I run a script at boot?

  3. Javier

    8 years ago

    The problem is that running programs looks for files directly in the root, not in the program directory. How it is possible to do it in the executable directory?

  4. electron1979

    8 years ago

    Thanks heaps.
    Did the “Auto run …” work for you? I get “su: must be run from a terminal”, whether I put it in rc.local or a *.sh.

    To test changes in rc.local, in a terminal type: /etc/rc.local It runs as root on boot-up, but as normal user when testing using /etc/rc.local.

  5. valki

    9 years ago

    Hello great tutorial,since i`m a little newbie into this,what i`m trying to do is run upon the boot of raspberry my assault cube server automatically,how can i do that please?

    the commands to run in manual are these:

    cd /usr/src/assault/AC
    sh server.sh

    1. LjMario007

      8 years ago

      You would add:

      sudo /usr/src/assault/AC/server.sh

      if you wanted the server to be run as root or

      /usr/src/assault/AC/server.sh

      if you wanted the server to be run as normal pi user.

  6. R

    9 years ago

    Is there a way to test changes to rc.local to see if the added commands work? My changes to it are not working. I don’t know how to see why. Can you test rc.local from an ssh command line?

  7. Patrick Scanlon

    9 years ago

    I cannot get this to work. Tried adding a simple app that I can get to successfully run within .bashrc, but I was trying to get this working so I didnt have to log in.. Would there be a reason it doesnt work in rc.local, but in .bashrc? I am using the raspbian OS. Thanks!

  8. mot86

    9 years ago

    the python script starts, but the tkinter window is not showing ?

  9. pi.minecraft

    9 years ago

    not working when i boot it says failed to load

  10. kkyang

    3 years ago

    thanks for the help. Anyway, if I want to run iw.c automatically, but at the same time I need to input parameters, the input order is like: ./iw 10 3, 10 is like iterations, 3 means location. How should I do it?

    1. HardwareHacks

      3 years ago

      if iw.c is CLI program, make the 10, ‘/path/to/data.file’ as default arguments. When autorunning, if the CLI program, the bootloader will hang waiting on user input.

  11. clayton

    3 years ago

    thanks for the help. How would you add a second program to boot up as well?

Comments

Your email address will not be published. Required fields are marked *