Raspberry PI, run script / program on startup

First create an executable script (my-script.sh):

vim /home/christopher/my-script.sh

Add the following code:

#!/bin/sh
# my-script.sh

# Make sure to use absolute paths in the scripts
# rather than relative paths.

touch /home/christopher/hello.md

echo "Hello World!" >> /home/christopher/hello.md

exit 0

Edit rc.local file:

sudo vim /etc/rc.local

Add new line to execute the script:

...
/bin/sh /home/christopher/my-script.sh

exit 0

If the script includes a loop or it is meant to be executed indefinitely, we might need to add and ampersand at the end of the line:

...
/bin/sh /home/christopher/my-script.sh &

exit 0

Finally, grant executable privileges to /etc/rc.local file and reboot the raspberry pi:

sudo chmod +x /etc/rc.local
sudo reboot -h now
Five Ways to Run a Program On Your Raspberry Pi At Startup (dexterindustries.com): Raspberry PI, run script / program on startup

Leave a Comment

Scroll to Top