A super-simple way to run scripts on boot

Here's a super-simple way to run scripts automatically on boot on your Raspberry Pi, using cron, that you can have up and running in literally a few seconds.

Cron

The secret sauce here is cron. Cron is a Unix program for scheduling jobs, and is incredibly versatile in terms of what it can do. Need to run a script every 15 minutes? Need to run a script a 6:05pm every day? Need to run a script at 20 minutes past every hour? Cron can handle it.

However, we won't be using those time-based features of cron here. We'll use its @reboot feature to run a python script whenever your Pi boots.

Blinkt! rainbows!

Let's say that you want to build a little rainbow light with a Blinkt! and a Pi Zero, and have it run our rainbow animation example whenever it's plugged in and turned on, without having to plug in a keyboard, mouse and display to open the terminal and run the script.

This assumes that you've already installed our Blinkt! library. You can follow our Getting Started with Blinkt! tutorial to get you up to speed.

Editing your crontab

Open a terminal, and type crontab -e. If it's the first time that you've edited your crontab, then it will ask you to choose which editor you'd like to use. I prefer to use nano, and this is the default, so you can just press enter to select it.

At the top of the crontab, you'll see a lot of blurb about how to use it, on lines that are commented out (they begin with #). Scroll all the way down past these lines to the bottom (using the down arrow key) and type, on the last line, the following:

@reboot python /home/pi/Pimoroni/blinkt/examples/rainbow.py &

We're using the full path to the script, all the way from the root. Last, we add & to the end to run the script in the background, so that the Pi will boot as normal.

Once you've added that line, press control-x, y and enter to exit nano.

It should say crontab: installing new crontab assuming you saved the crontab successfully.

Type sudo reboot to reboot your Pi, and the rainbow example should run after a few seconds.

To remove the @reboot command, just type crontab -e again, delete the offending line, and then exit and save as before.

Shopping basket

Need something for this project? You can use the links below to add products to your Pimoroni Shop basket for easy checkout.

Raspberry Pi Zero
Pi Zero only £4.80
Raspberry Pi Zero
Pi Zero + Adaptors £9.00
Raspberry Pi Zero
Pi Zero + Adaptors + Pibow Zero Case £13.00
Raspberry Pi Zero W
Zero W only £9.60
Raspberry Pi Zero W
Zero W + Adaptors £14.00
Raspberry Pi Zero W
Zero W + Adaptors + Pibow Zero W £19.00
Pi Zero W Starter Kit
£32.00
Blinkt!
£5.00
Want to checkout or change something? Click here to view your cart.

Tutorial
Beginner
Raspberry Pi

Sandy Macdonald

sandy@pimoroni.com
@sandyjmacdonald
http://sandyjmacdonald.github.io
Formerly, Sandy worked at the University of York, in the biology department, analysing data and telling people that they should have used more replicates. Now a fully-fledged crew member at Pimoroni - head of digital content - working on learning materials and digital chunterings. Find him on Twitter and most everywhere else, as sandyjmacdonald.