Auto running terminal applications (non GUI) on Raspberry PI

Setup

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/motioneye.py > /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.

Author: Helicopter Jeff

I'm a vinyl decal cutting and designing, Astrophotography and general photography capturing, RC helicopter/quad copter pilot, Arduino and Raspberry Pi hardware/software developer, network, scripting and troubleshooting ninja living in the metro NY area. I am passionate about my hobbies so much I do them for a living. There's nothing better than getting paid to do what you love.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s