Raspberry Pi for N00bs – Startup Scripts

You already know how to run scripts periodically with a cron job if you read my last blogpost about the Raspberry Pi. This time i will explain how to run scripts on startup. Because you do want to restart your Minecraft server when the Raspberry Pi crashes, right? 😉

There are several ways to create startup scripts, I will only explain the easiest and most common one.

/etc/rc.local

This is not a folder, but a file that will get run whenver you boot/reboot your Raspberry Pi. So let’s open that file with our beloved nano editor:

Whatever you add to that file, make sure to add it BEFORE “exit 0“. That one should always be the very last line. You do not need to add “sudo” to the calls, because all commands will get called as root user. One more important thing to know is that you need to use the full path, because environmental variables are not set when those commands will get called.

So this is what it would look like if you would want to call a python script on startup:

By default there already are some lines of code in the file, so this is what the whole file looks like with the python script call:

…and that’s pretty much all there is to know. Next time your Raspberry Pi boots, the script will get executed. You can also call your own shell scripts:

Just make sure to make them executable by using chmod +x, as explained in the article about Cron Jobs. The Ampersand (“&”) means that the start script will not wait for the script and go to the next line immediately.

One more important thing to know is how to log errors. Make sure rc.local works correctly BEFORE you reboot your Raspberry Pi, you can do that by just calling it directly in the shell:

This is the best way to check if your entries worked and to see if there are any errors.

That´s it about startup scripts so far, if you got any questions or tips, feel free to comment!

2 thoughts on “Raspberry Pi for N00bs – Startup Scripts

  1. Javier Pedreira (Wicho) says:

    Hi.

    I’m trying to get an app exported from Processing 3 to run at startup on a Raspberry Pi 3 Model B. with Raspbian GNU/Linux 8 (Jessie). The application is at

    /home/pi/sketchbook/LACTLC/application.linux64/LACTLC

    I’ve followed your instructions and added the full path to rc.local, but it refuses to load at startup, even though if I run /etc/rc.local from the terminal it works fine.

    Any suggestions as to what I might try?

    Thanks in advance.

Leave a Reply

Your email address will not be published. Required fields are marked *