3 Ways to Run Program at boot

Hi All,

Today, I’m going to show how to run a python program at boot or startup. There are three ways to do that:

Way 1 : init.d directory

You can create python program and add to the /etc/init.d directory.

  1. Create a python program name sample.py.
$ sudo nano sample.py

2. Write below coding. This coding is for LED blinking at pin 14.

#!/usr/bin/python3
# /etc/init.d/sample.py
### BEGIN INIT INFO
# Provides:          sample.py
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     S 2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable service provided by daemon.
### END INIT INFO
#from subprocess import call
#call(['espeak "Welcome to the world of Robots" 2>/dev/null'], shell=True)

#DAEMON=/usr/bin/mplayer
import os
import time
#time.sleep(3)
#os.system("/usr/bin/mplayer /home/pi/0003.mp3")

import RPi.GPIO as GPIO

led = 14

GPIO.setmode(GPIO.BCM)
GPIO.setup(led, GPIO.OUT)
GPIO.output(led, GPIO.LOW)
   
while True:
    GPIO.output(led, GPIO.HIGH)
    time.sleep(1)
    GPIO.output(led, GPIO.LOW)
    time.sleep(1)

3. Copy that file to /etc/init.d directory by type this command:

$ sudo cp /home/pi/sample.py /etc/init.d/

4. Make the sample script in the init directory executable by type this command:

$ cd /etc/init.d/
$ sudo chmod +x sample.py

5. Run this command:

$ sudo update-rc.d sample.py defaults

6. Reboot the raspberry pi.

$ sudo reboot -f

7. You will LED blinking at start up. Done !!!

Way 2 : crontab

You can create python program and add to the crontab.

  1. Create a python program name testsample.py.
$ sudo nano testsample.py

2. Write below coding. This coding is for LED blinking at pin 14.

#!/usr/bin/python3
import os
import time
#time.sleep(3)
#os.system("/usr/bin/mplayer /home/pi/0003.mp3")

import RPi.GPIO as GPIO

#from subprocess import call
#call(['espeak "Welcome to the world of Robots" 2>/dev/null'], shell=True)

led = 14

GPIO.setmode(GPIO.BCM)
GPIO.setup(led, GPIO.OUT)
GPIO.output(led, GPIO.LOW)

while True:
    GPIO.output(led, GPIO.HIGH)
    time.sleep(0.1)
    GPIO.output(led, GPIO.LOW)
    time.sleep(0.1)

3. Open the terminal and type this command (no sudo):

$ contab -e

4. In crontab, type this command:

XDG_RUNTIME_DIR=/run/user/1000
DISPLAY=:0

@reboot cd /home/pi && /usr/bin/python3.7 testsample.py

5. Reboot the raspberry pi.

$ sudo reboot -f

6. You will LED blinking at start up. Done !!!

Way 3 : SYSTEMD

systemd provides a standard process for controlling what programs run when a Linux system boots up.

  1. Create A Unit File by type this command:
$ sudo nano /lib/systemd/system/sample.service
2. Add the following coding:
 [Unit]
 Description=My Sample Service
 After=multi-user.target

 [Service]
 Type=idle
 ExecStart=/usr/bin/python /home/pi/sample.py

 [Install]
 WantedBy=multi-user.target

3. Change the permission unit by type this command:

$ sudo chmod 644 /lib/systemd/system/sample.service

4. Tell systemd to start it during the boot sequence type this command:

$ sudo systemctl daemon-reload
$ sudo systemctl enable sample.service

5. Reboot the raspberry pi.

$ sudo reboot -f

6. You will LED blinking at start up. Done !!!