Categories:

Raspberry Pi auto-start script

There are three ways to start a script or program on startup.

Env: Raspberry Pi 3, 4 (Debian 10 or Raspbian Buster)

Method 1 with rc.local

sudo nano /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.
/home/pi/Desktop/startChrome.sh

exit 0

// Done

chmod +x /etc/rc.local

// Enable rc.local on startup
systemctl enable rc.local

Method 2 with systemd

sudo nano /etc/systemd/system/autostart.service

// Paste this into it
[Unit]
Description=Auto keepalive daemon
## make sure we only start the service after network is up
Wants=network-online.target
After=network.target

[Service]
## use 'Type=forking' if the service backgrounds itself
## other values are Type=simple (default) and Type=oneshot
Type=forking
## here we can set custom environment variables
Environment=AUTOSSH_GATETIME=0
Environment=AUTOSSH_PORT=0
ExecStart=/home/pi/Desktop/startChrome.sh
ExecStop=/usr/bin/killall -9 autostart
### NOTE: you can have multiple `ExecStop` lines
ExecStop=/usr/bin/killall autostart
# don't use 'nobody' if your script needs to access user files
# (if User is not set the service will run as root)
#User=nobody

# Useful during debugging; remove it once the service is working
StandardOutput=console

[Install]
WantedBy=multi-user.target

// END

// Make it autostart 
sudo systemctl enable autostart.service

Done and Reboot

Method 3 start after LXDE desktop is loaded

// Make an auto-start directory
mkdir /home/pi/.config/autostart

// Put your script in it
nano autoChrome.desktop

[Desktop Entry]
Type=Application
Name=Video auto play
Exec=/home/pi/Desktop/startChrome.sh

Done and reboot 

If you got (code=exited, status=203/EXEC) error, you have to add #!/bin/sh at the very beginning in your .sh script.