/etc/systemd/system/myscript.service, point ExecStart at your Python and script with absolute paths, then run sudo systemctl enable --now myscript.service. Check it with systemctl status and journalctl -u.You wrote a Python script on your Raspberry Pi and want it to start automatically every time the Pi boots, and restart if it crashes. The clean way to do it is a systemd service. It takes two minutes.
1. Make Sure the Script Runs by Hand
python3 /home/pi/myproject/main.py
Replace the path with yours. If your username is not pi, check it with whoami. Fix any errors now: a service only hides them.
2. Create the Service File
sudo nano /etc/systemd/system/myscript.service
Paste this and change the paths and user:
[Unit]
Description=My Python script
After=network-online.target
Wants=network-online.target
[Service]
User=pi
WorkingDirectory=/home/pi/myproject
ExecStart=/usr/bin/python3 /home/pi/myproject/main.py
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
- After / Wants network-online: waits for the network, important if your script uses Wi-Fi or the internet.
- User: runs as your user, not root.
- WorkingDirectory: relative file paths in your script start from here.
- Restart=on-failure: if the script crashes, systemd starts it again after 5 seconds.
Using a virtual environment? Change ExecStart to /home/pi/myproject/venv/bin/python /home/pi/myproject/main.py.
3. Enable and Start It
sudo systemctl daemon-reload
sudo systemctl enable --now myscript.service
enable makes it start on boot, --now also starts it right away.
4. Check That It Works
systemctl status myscript.service
journalctl -u myscript.service -f
status should say active (running). journalctl -f shows your script’s output live, including print() lines and errors. Reboot with sudo reboot to confirm it comes back by itself.
Common Problems
- ModuleNotFoundError: the service uses a different Python than you. Use the full path of the right interpreter or your venv.
- FileNotFoundError: use absolute paths or set
WorkingDirectory. - No output in the logs: Python buffers output. Add
Environment=PYTHONUNBUFFERED=1under[Service]. - Needs GPIO or hardware: make sure your user is in the right groups, for example
gpioori2c.
If your Pi will run 24/7, also keep an eye on heat and power: give it a static IP so you can always reach it over SSH.
🛒 What you need
As an Amazon Associate, Arelvox earns from qualifying purchases. This does not change the price you pay.
Frequently Asked Questions
Why not use rc.local or crontab @reboot?
They work, but systemd is the standard on Raspberry Pi OS. It can wait for the network, restart your script if it crashes, and keeps logs you can read with journalctl.
My script works by hand but not as a service. Why?
Usually paths. A service does not start in your home folder and may not have your environment. Use absolute paths for the script, files it opens and the Python interpreter, and set WorkingDirectory.
How do I use a virtual environment?
Point ExecStart at the Python inside the venv, for example /home/pi/myproject/venv/bin/python. That interpreter already uses the venv packages.
How do I stop it from starting on boot?
Run sudo systemctl disable myscript.service. To stop it right now, use sudo systemctl stop myscript.service.
3 thoughts on “How to Run a Python Script on Boot on Raspberry Pi (systemd)”