Raspberry Pi: Launch Python script on startup

As I've been working on my own Pi projects, I've been discovering many little tricks and tips by scouring various websites and assembling information, testing and optimizing.

So, here is another one of my “meat-and-potatoes” Raspberry Pi Instructables.

This Instructable will show you how to setup your Raspberry Pi to automatically launch a Python script upon startup.

First of all, I know this is a lame picture. If you can come up with a better one, I'm open to suggestions.

The Arduino has auto-launch built into it; the Pi does not. This will make your Pi a more powerful electronics platform and is essential if, for example, you want to use your Pi as a video kiosk using GPIO controls.

Before doing this Instructable, please make sure you have your Raspberry Pi up and running, which you can do with The Ultimate Raspberry Pi Configuration Guide Instructable.

I'm using the Mac OS for this guide, but you can extend the principles to other operating systems.Raspberry Pi: Launch Python script on startup

Step 1: Make a launcher sript

I'm building out a new project called Black Box Timelapse (Instructable coming soon…).

My python script is called : bbt.py and lives in a directory called bbt that is in the root directory. You can substitute your own director/Python script name instead of using mine.

We will use the Linux crontab to run the Python script.

I've had trouble with crontab and directory management and my solution is to amke a shell script, which always navigates to the proper directory and will launch my bbt.py Python script.

Let's create the shell script!

I'm using ssh to access to Raspberry Pi. My IP address for the SD card for this is 10.0.1.68. Your IP address may be different — just change the address accordingly.

Open the Terminal window and on the command line, type:

ssh [email protected]

If you are running directly hooked into the monitor, you can skip this step.

Type in:

cd bbt

then:

nano launcher.sh

Will launch your editor, type in this script

#!/bin/sh
# launcher.sh
# navigate to home directory, then to this directory, then execute python script, then back home

cd /
cd home/pi/bbt
sudo python bbt.py
cd /

Cntl-X, Return to save.

What this script will do is to navigate to the root directory, then to the bbt directory, launch the Python script and then return to the root directory.

Step 2: Make it executable

We need to make the launcher script an executable, which we do with this command

chmod 755 launcher.sh

Now test it, by typing in:

sh launcher.sh

This should run your Python code.

Step 3: Add logs directory

We will get to using crontab in a minute, but first we need to make a directory for the any errors in crontab to go.

Navigate back to your home directory:

cd

Create a logs directory:

mkdir logs

Step 4: Add to your crontab

crontab is a background (daemon) process that lets you execute scripts at specific times. It's essential to Python and Raspberry Pi. The details are confusing, as is often the case with Linux. Once I got the hang of the format, I've found it to be incredibly easy to use.

Here's the Linux reference and here are some more crontab examples.

Type in:

sudo crontab -e

This will brings up a crontab window.

Now, enter the line:

@reboot sh /home/pi/bbt/launcher.sh >/home/pi/logs/cronlog 2>&1

What this does is rather than executing the launcher script at a specific time, it will execute it once upon startup.Raspberry Pi Launch Python script on startup schematic.jpg

Step 5: Reboot and see if it works

Unplug the power or just type in:

sudo reboot

Wait for startup and see if your script automatically launches.

If it doesn't work, check out the log file:

cd logs
cat cronlog

This will show you any errors that you might have.

Step 6: Always make an exit plan!

You'll want to have an easy way to exit the code, so that you don't get stuck in an endless buggy loop.

The way I do this is to plug in the keyboard (not plugged in for standard kiosk usage) and exit the script this way.

In a standard Python script, you can always hit cntl-C, which will exit.

If using the pygame libraries, you can do an exit on keydown, such as:

while not done:
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            done = True

 

For more detail: Raspberry Pi: Launch Python script on startup


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

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

Scroll to Top