Running Pi Projects as Services: the short answer
A script started from an SSH session stops when you log out or the Pi reboots. For a project that must keep running, make it a systemd service.
- Level
- Advanced
- Time
- About 6 min
- Topic
- Raspberry Pi
Make a Python project start at boot, restart after a crash, log properly and survive power cuts.
Before this: I2C Sensors on a Raspberry Pi, Measurement Logging & Calibration
A script started from an SSH session stops when you log out or the Pi reboots. For a project that must keep running, make it a systemd service. Create /etc/systemd/system/sensor-logger.service with three sections. [Unit] has Description and After=network-online.target with Wants=network-online.target if it needs the network. [Service] has User=yourname, WorkingDirectory=/home/yourname/logger, ExecStart=/home/yourname/env/bin/python -u logger.py, Restart=on-failure and RestartSec=5. [Install] has WantedBy=multi-user.target.
Then run sudo systemctl daemon-reload and sudo systemctl enable --now sensor-logger. Check it with systemctl status sensor-logger and follow its output with journalctl -u sensor-logger -f. The -u flag on python turns off output buffering, so print lines appear in the journal immediately instead of in bursts.
Plan for power cuts, which are common. SD cards wear out and can corrupt if the Pi writes constantly, so write logs in batches, keep the data on an NVMe SSD or send it to a server, and consider the read-only overlay file system in raspi-config for kiosk-style projects. Write your code so it can start again at any point: open files in append mode, reconnect to Wi-Fi and MQTT in a loop, and never assume the previous run finished cleanly.
A unit file describes the job
A service is described in a unit file under /etc/systemd/system. The main parts are ExecStart, the full path of the command; User, so it does not run as root without need; WorkingDirectory; and Restart=on-failure so it comes back if it crashes. After editing, run systemctl daemon-reload, then enable the service to start at boot and start it now.
Use full paths everywhere, including the Python interpreter and the script. A service does not have your login's environment, so a relative path or a variable set in your shell is the usual reason it works by hand and fails as a service.
Order and logs
If the program needs the network, add After=network-online.target and Wants=network-online.target. A program that needs a mounted drive or another service has similar dependency lines. Read the logs with journalctl -u name -f, which shows output and errors from the service.
Add a restart delay with RestartSec, so a program that fails immediately does not restart in a tight loop. For a project that must keep running, the hardware watchdog can reboot the Pi if it hangs.
Safe habits
Test the program by hand first, then as a service. Keep the SD card healthy with a good power supply, and store logs with limits so they do not fill the card.
- Using relative paths, so the service cannot find its files.
- Not waiting for the network, so the program fails at boot.
- Forgetting daemon-reload after editing the unit file.
Practise this lesson with Pro
The explanation above is free for everyone. The practice for Advanced lessons in every domain are part of the Pro plan at ₹999 a month.
With Pro you also get: a worked example, a hands-on task, a 3-question check.
