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.
[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.targetSave this file with sudo nano /etc/systemd/system/myproject.service (typing the content above), then enable and start it:
sudo systemctl daemon-reload
sudo systemctl enable myproject.service
sudo systemctl start myproject.serviceExecStart 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.