🔧 Creating and Understanding a systemd Service File on Raspberry Pi

A systemd service file is used in Linux (including Raspberry Pi OS) to define how a service (like a Python script or web server) should be started, stopped, restarted, and managed.
Here’s the basic structure of a .service file:
[Unit]
Description=Your Service Description
After=network.target
[Service]
ExecStart=/usr/bin/python3 /path/to/your/script.py
WorkingDirectory=/path/to/your/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
🔍 Section Breakdown
[Unit]
Description=: Human-readable description of the service.After=: Specifies service start order. Commonly set tonetwork.targetif the service depends on networking.
[Service]
ExecStart=: The full command to run your service. Use full paths!WorkingDirectory=: Directory where the script will be run.StandardOutput=andStandardError=: Where logs are directed.inheritpasses it to the system journal.Restart=: Automatically restart the service if it crashes. Usealwaysoron-failure.User=: Specifies the user under which the service will run (commonlypion Raspberry Pi).
[Install]
WantedBy=multi-user.target: Tells systemd to start the service at boot in the typical multi-user (non-graphical) mode.
Example: Blink LED Python Script Service
Let's say you have a script: /home/pi/blink.py
[Unit]
Description=Blink LED on Raspberry Pi
After=network.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/blink.py
WorkingDirectory=/home/pi/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
Save this file as /etc/systemd/system/blink.service
To Use It:
sudo systemctl daemon-reload # Reload systemd manager configuration
sudo systemctl enable blink.service # Enable on boot
sudo systemctl start blink.service # Start the service
sudo systemctl status blink.service # Check its status
To create a Linux systemd service file that runs your blink.py Python script on a Raspberry Pi (e.g., at boot), follow these steps:
✅ 1. Sample blink.py script
Make sure your script is executable and working:
# blink.py
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
try:
while True:
GPIO.output(17, GPIO.HIGH)
time.sleep(1)
GPIO.output(17, GPIO.LOW)
time.sleep(1)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()
Save this file at /home/pi/blink.py (or wherever you'd like).
✅ 2. Make it executable
chmod +x /home/pi/blink.py
✅ 3. Create a systemd service file
Create a new service file:
sudo nano /etc/systemd/system/blink.service
Paste the following content:
[Unit]
Description=Blink LED using Python script
After=multi-user.target
[Service]
ExecStart=/usr/bin/python3 /home/pi/blink.py
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit
Restart=always
User=pi
[Install]
WantedBy=multi-user.target
Note: If you're using a virtual environment, replace
/usr/bin/python3with the path to your virtual environment's Python binary.
✅ 4. Enable and start the service
sudo systemctl daemon-reload
sudo systemctl enable blink.service
sudo systemctl start blink.service
✅ 5. Check status
sudo systemctl status blink.service
✅ 6. Logs (optional)
If your script prints something or crashes:
journalctl -u blink.service -f
To disable or stop your blink.service on Raspberry Pi (or any Linux system using systemd):
🛑 To stop the service (immediately):
sudo systemctl stop blink.service
This halts the running service but does not prevent it from starting at boot.
🚫 To disable the service (prevent it from starting at boot):
sudo systemctl disable blink.service
You can combine both:
sudo systemctl stop blink.service
sudo systemctl disable blink.service
✅ To re-enable later:
sudo systemctl enable blink.service
sudo systemctl start blink.service

