ABC of Electronics cosycom.com
RASPBERRY PI 18 · Automation

Running a Script Automatically at Boot

Turn a working script into a real background service that starts itself automatically every time the Pi powers on.

You'll need: a working Python script (any from this course), and your Pi.

Every script so far has been started manually from the terminal — fine for development, but a finished project usually needs to start itself the moment the Pi powers on, with no one there to type a command. The standard, reliable way to do this on Raspberry Pi OS is a systemd service.

/etc/systemd/system/myproject.service
[Unit]
Description=My GPIO Project
After=multi-user.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/myproject.py
Restart=on-failure
User=pi

[Install]
WantedBy=multi-user.target

Save this file with sudo nano /etc/systemd/system/myproject.service (typing the content above), then enable and start it:

terminal
sudo systemctl daemon-reload
sudo systemctl enable myproject.service
sudo systemctl start myproject.service

ExecStart is the exact command to run, using full paths rather than relative ones, since a boot-time service has no "current directory" the way an interactive terminal session does. Restart=on-failure automatically restarts your script if it crashes. enable registers it to start at every future boot; start runs it immediately, without waiting for a reboot, so you can confirm it works right away with sudo systemctl status myproject.service.

Pi powers on systemdstarts enabled services Your scriptrunning, no login neededabcofelectronics
A systemd service bridges power-on directly to your script running — no terminal session or manual login required.
TRY THIS
Use full absolute paths in a systemd service's ExecStart line — boot-time services have no working directory context, so a relative path that works from your terminal will fail here.