Running a python script when a Raspberry Pi boots up.

Andy Burdett
2 min readDec 11, 2020

If like me, you’ve spent the last twelve months working from home and only seeing your work colleagues via Zoom, Google Meet or Microsoft Teams you’ve probably seen lots of poorly lit desk spaces and shadowy figures giving updates to your daily stand up meeting.

I was one of those individuals until I dug out an old Raspberry Pi with it’s Pimoroni unicorn hat. I thought to myself it would be cool to have all the LEDs light up and emit a bright white light when it booted so that my colleagues could see who was giving the update.

Pimoroni have a vast array of boards you can use with the Raspberry Pi. They also produce libraries and examples of how to use their boards.

For this task I reused the simple.py python program tweaked a few parameters for the colour and brightness so that all the LEDs were white.

sudo simple.py

Simple right?

How would I get this python script to run when the Raspberry Pi booted up? Also how to run it without the sudo command?

The solution I came up with was to add the script to the /etc/rc.local file. Because this is executed at boot and as root sudo is not required. Using nano I added the following line just before the exit 0

python /home/pi/Pimoroni/unicornhat/examples/simple.py &

The & at the end of the line is required so that the python command is spawned in it’s own process and the rc.local can exit.

Reboot and you’re done.

--

--