Running a Script on Startup for Raspberry Pi

Protonest IoT
3 min readAug 8, 2024

--

Running a Script on Startup for Raspberry Pi

You may probably have a few scripts that you always want to run when your Raspberry Pi boots up. Automating the strict will help you to run the code only by powering the Raspberry Pi.

In this article, we’re going to show you a simple and effective way to get your script running automatically on startup using ‘cron’.

Hardware Required

What is ‘cron’?

‘cron’ is a powerful tool that allows you to schedule tasks on your Raspberry Pi. You can set it to run tasks at specific times or intervals, like in this article, running it at startup. Think of it as your personal task scheduler that takes care of running your scripts.

Let’s Get Started!

For this tutorial, we’re going to use a sample Python script located at ‘/home/protonest/Desktop/Sample/blink.py’. Feel free to replace this with the path to your script.

The blink.py script we are using is,

import RPi.GPIO as GPIO
import time

# Pin Definitions
led_pin = 17 # GPIO pin 17

# Pin Setup
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
GPIO.setup(led_pin, GPIO.OUT) # LED pin set as output

print("Starting LED blink script. Press Ctrl+C to exit.")

try:
while True:
GPIO.output(led_pin, GPIO.HIGH) # Turn LED on
time.sleep(1) # Sleep for 1 second
GPIO.output(led_pin, GPIO.LOW) # Turn LED off
time.sleep(1) # Sleep for 1 second
except KeyboardInterrupt:
print("Script interrupted. Cleaning up...")
finally:
GPIO.cleanup() # Clean up all GPIO

Step-by-Step Guide

  • Open the Terminal: Power up your Raspberry Pi and open the terminal. You can do this either through the desktop interface or via SSH/VNC if you’re connecting remotely.
  • Edit the Crontab File: Enter the following command to open the crontab editor,
sudo crontab -e
Screenshot for sudo crontab -e

This command opens the crontab file for the root user, allowing you to schedule tasks that need root privileges. If you’re prompted to choose an editor, you can select your preferred one (I usually go with nano for its simplicity).

  • Add the Startup Command: Scroll to the bottom of the crontab file and add the following line:
@reboot /home/protonest/Desktop/Sample/blink.py &
Screenshot for adding @reboot /home/protonest/Desktop/Sample/blink.py &

Let’s break down what this line does:

‘@reboot’: This is the special time string that tells ‘cron’ to run the command at startup.

‘/home/protonest/Desktop/Sample/blink.py’: This is the path to your script.

‘&’: The ampersand at the end runs the script in the background, so it doesn’t block other startup processes.

  • Save and Exit: Once you’ve added the line, save and exit the editor. If you’re using nano, you can do this by pressing ‘Ctrl + X’, then ‘Y’ to confirm, and ‘Enter’ to save.
  • Test It Out: To make sure everything is set up correctly, reboot your Raspberry Pi,
sudo reboot

Once your Pi boots back up, your script should start running automatically!

And there you have it! You’ve successfully set up your Raspberry Pi to run a script on startup using ‘cron’.

This is a super handy trick that can be used for all sorts of projects. Imagine having your Pi automatically start up a web server, monitor sensors, or even play a custom greeting message when it powers on. The possibilities are endless!

If you run into any issues or have any questions, feel free to drop a comment below.

Pro Tip: If your script doesn’t run as expected, double-check the path and permissions. Sometimes, scripts need executable permissions to run. You can add executable permissions with,

chmod +x /home/protonest/Desktop/Sample/blink.py

Contact us for any consultations or projects related to Raspberry Pi.

Email: udara@protonest.co

Protonest for more details.

Protonest specializes in transforming IoT ideas into reality. We offer prototyping services from concept to completion. Our commitment ensures that your visionary IoT concepts become tangible, innovative, and advanced prototypes.

Our Website: https://www.protonest.co/

Cheers!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Protonest IoT
Protonest IoT

Written by Protonest IoT

We make your IoT ideas a reality

No responses yet

Write a response