Pages

2020/02/17

Raspberry Pi - How to run a program on startup

This is a quick write up of me following the instruction here to test out how to use the GPIO of Raspberry Pi and make a program to auto-execute on startup.

Wiring Diagram

Wire up the Pi with an LED and a resistor as shown below.


On your Raspberry Pi, open a terminal, and make sure you are in your home directory:

cd /home/pi

Open a new document named blink.py

nano blink.py

The Code (blink.py)

import time
import RPi.GPIO as GPIO

# Pin definitions
led_pin = 12

# Use "GPIO" pin numbering
GPIO.setmode(GPIO.BCM)

# Set LED pin as output
GPIO.setup(led_pin, GPIO.OUT)

# Blink forever
try:
    while True:
        GPIO.output(led_pin, GPIO.HIGH) # Turn LED on
        time.sleep(1)                   # Delay for 1 second
        GPIO.output(led_pin, GPIO.LOW)  # Turn LED off
        time.sleep(1)                   # Delay for 1 second

# When you press ctrl+c, nicely release GPIO resources
finally:
    GPIO.cleanup()

Save it with ctrl + x, then press y when asked to save, and press enter. Run the program with the following:

python blink.py

You should see the LED begin to blink on and off. Press ctrl + c to stop the program.

Make the program to auto start on bootup using the rc.local method.

Running your program from rc.local is likely the easiest method, but because rc.local is executed before X starts, you will not have access to GUI elements. As a result, it's recommended that you only use rc.local for starting programs that do not have graphical elements.

The rc.local script is executed after all of the normal system services have been started (including networking, if enabled) and just before the system switches to a multiuser run level (where you would traditionally get a login prompt). While most Linux distributions do not need an rc.local, it's usually the easiest way to get a program to run on boot with Raspbian.

Modify rc.local

You will need root-level access to modify rc.local, so do so with sudo:

sudo nano /etc/rc.local

Scroll down, and just before the exit 0 line, enter the following:

python /home/pi/blink.py &

pi@raspberrypi:~ $ cat /etc/rc.local
#!/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
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

python /home/pi/blink.py &

exit 0

Note: the ampersand (&) at the end of the line is needed so that your Python script runs in a different process and does not block execution of the boot process. Without it, the rc.local script would wait for your script to end before continuing, and if you have an infinite loop (like in our blink.py program), you would never get a login prompt.

Notice that we are calling our script with the absolute file location (/home/pi/blink.py), as calling python blink.py from within rc.local will cause python to look for a local file (i.e. blink.py file located in the same directory as rc.local). We use the absolute file location to make it explicit to Python where our program can be found.

Save and exit with ctrl + x, followed by y when prompted to save, and then enter.

Test it by restarting your Pi with sudo reboot. You should see the LED begin to blink on and off after the system reboot.

How to stop your program

You might notice that your program runs great, but there's no easy way to stop it! The simplest method would be to remove (or comment out) the line you added in rc.local followed by a reboot, but that takes a lot of time.

The quickest way to stop your program is to kill its Linux process. In a terminal, enter the following command:

sudo ps -ax | grep python

ps -ax tells Linux to list out all the current processes. We pipe that list to grep, which allows us to search for keywords. We're looking for python in this example, but feel free to change it to the name of your program or whatever you are using to run your program.

pi@raspberrypi:~ $ sudo ps -ax | grep python
  420 ?        S      0:00 python /home/pi/blink.py
  675 pts/0    S+     0:00 grep --color=auto python
pi@raspberrypi:~ $

Find the process ID (PID) number to the left of the listed process, and use the kill command to terminate that process:

sudo kill <PID>

In the example above, the PID is 420. Issuing "sudo kill 420" will immediately kill the program and stop the blinking of the LED.

IMPORTANT! Make sure you type the PID correctly! If you kill the wrong process, you could halt Linux, and you would need to reboot again.

How to Stop Your Program from Running on Boot

If you no longer want your program to run on boot, simply open rc.local with:

sudo nano /etc/rc.local

Delete the line that you added to run your program, and save and exit with ctrl + x and y. Reboot your computer, and your program should no longer run after boot.

Reference:

How to Run a Raspberry Pi Program on Startup
https://learn.sparkfun.com/tutorials/how-to-run-a-raspberry-pi-program-on-startup/all

No comments:

Post a Comment