Skip to main content

Command Palette

Search for a command to run...

🔧 Creating and Understanding a systemd Service File on Raspberry Pi

Updated
3 min read
🔧 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 to network.target if 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= and StandardError=: Where logs are directed. inherit passes it to the system journal.

  • Restart=: Automatically restart the service if it crashes. Use always or on-failure.

  • User=: Specifies the user under which the service will run (commonly pi on Raspberry Pi).

[Install]

  • WantedBy=multi-user.target: Tells systemd to start the service at boot in the typical multi-user (non-graphical) mode.

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/python3 with 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

More from this blog

Aarav's Blogs

19 posts

🔧 Creating and Understanding a systemd Service File on Raspberry Pi