Sam Westby

How to Run a Script on Startup for the Raspberry Pi

Just power on your Pi and let a script magically run in the background. This is a beginner tutorial that will put you on the path to making expert projects.

Samuel Westby

May 30th, 2022

# raspberry pi

There are a bunch of ways to run a script on startup with a Raspberry Pi. I find crontab the best. It's easy and straightforward which is more than I could ask for when I'm programming.

Supplies

Setup

I'm assuming you have your Pi setup and are able to access a terminal. It doesn't matter if you use ssh or the Pi as a desktop. You also need the script you want to run. In the video and in this tutorial, I use a script called buzzer.py. It activates a buzzer that I have connected to my Pi's GPIO pins.

Initialize crontab

In the terminal, run

 sudo crontab -e 

You may get a menu that looks like the picture below. We'll select option 1.

crontab options on first use

Edit crontab

Now you're editing the crontab file in the editor Nano. Move your curser to the bottom of the file and add the following code:

@reboot YOUR_SHELL_COMMAND &

Replace "YOUR_SHELL_COMMAND" with any command that you can run in your terminal. This is what will be run on startup. Here's what mine looks like:

adding a command to the crontab page
Finally, Ctrl + S to save and Ctrl + X to exit.
IMPORTANT: Don't forget the "&" at the end.

Conclusion

That's it! Just reboot your Pi and it should work. If not, double check your shell command.
Lorem ipsum, dolor sit amet consectetur adipisicing elit. Libero tenetur non laborum dolorem laboriosam quo quibusdam assumenda dolores eveniet. Ipsum?