Run Programs On Your Raspberry Pi At Startup

Credit to this website:

Five Ways To Run a Program On Your Raspberry Pi At Startup

 

There are few ways to run a program on Raspberry Pi at Startup or we called auto login program.

We can run two or more programs simultaneously. Here I will show a tutorial on how to run two programs at Startup.

1) Method 1: File rc.local

We need to edit a file named rc.local. Below is the steps involved:

  • Open the terminal and type: sudo nano /etc/rc.local.
  • Add commands to execute your python programs. You must put full path of file program location.
  • Be sure to leave the line exit 0 at the end, then save the file and exit. In nano, to exit, type Ctrl-x, and then Y.

sudo python3 /home/pi/Desktop/my_program/LED.py &
sudo python3 /home/pi/Desktop/my_program/LED_blinking.py &

Example: LED.py is LED at GPIO18 will blink 5 times, LED_blinking.py will                                             blinking infinity. The ampersand (&) allows the command to run in a                                       separate process and continue booting with the main process running.

  • Save it. In nano, to exit, type Ctrl-x, and then Y and press ENTER.
  • And reboot : sudo reboot
  • You will see the both programs will running.
  • That’s It !!!

2) Method 2: Using Crontab

1. We need to create a python script

2. For example:

/home/pi/Desktop/my_program/LED.py &
/home/pi/Desktop/my_program/LED_blinking.py &

3. Add a new Cron job by modify “crontab”. Use this command to edit:

     sudo crontab -e

4. You should see something that looks like this :

Crontab Example - Python at Boot

5. Using your cursor keys scroll to the bottom and add the following line :

    @reboot python /home/pi/Desktop/my_program/LED.py &
    @reboot python /home/pi/Desktop/my_program/LED_blinking.py

*** Make sure you put new lines by press ENTER

6.To save these changes click “CTRL-X”, then “Y” and finally “Return”. You should now be back at the command prompt.

7.To start testing you can now reboot using :

    sudo reboot.

8.To stop the running program, do this command:

    ps aux | grep /home/pi/Desktop/my_program/LED.py

9.This should give you a line starting with “root” and ending in the path to your script. Immediately after the “root” should be a process number. For example :

    root  1863  0.0  1.0  24908  4012 ?  Sl  19:45  0:00  python3  /home/pi/LED.py

10. We can stop the process using :

    sudo kill 1863

11. Done