Auto running terminal applications (non GUI) on Raspberry PI


This info will likely work for general linux as well, but my focus here is on the Pi.

You can set it up 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

Running multiple items

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.

Running as a non root user

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_app.a is run in the background.

After a 5 second delay VLC is started to stream 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")&

For my purposes I want motionEye to run on a Raspberry Pi. This is the line I added to rc.local to accomplish this:

python /home/pi/motioneye/ > /home/pi/motioneye/run/motioneye.log 2>&1 &

In the case of the Pi, the last line of the rc.local is

exit 0

You want to ensure that your modifications are above this.

Leave a Reply

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