🧠 5. Raspberry Pi – Programming & Scripting
Estimated reading: 4 minutes 25 views

πŸ” Raspberry Pi – Services & systemd Usage (2025 Guide to Startup & Background Tasks)


🧲 Introduction – Control Your Raspberry Pi with systemd Services

When you need your Raspberry Pi to automatically start programs at boot, manage long-running processes, or recover from failure, Linux’s built-in systemd service manager is your best tool. With it, you can create reliable, background-running services for GPIO, sensors, servers, Python scripts, and more.

🎯 In this guide, you’ll learn:

  • What systemd is and why it’s used on Raspberry Pi
  • How to create, enable, and manage .service files
  • How to auto-run scripts on boot with logs and restart logic
  • Real-world project use cases

βš™οΈ What Is systemd and Why Use It?

systemd is the init system and service manager used by Raspberry Pi OS (and most modern Linux distros). It controls:

  • Startup and shutdown process
  • Scheduled or dependent service execution
  • Logs via journalctl
  • Background process supervision

βœ… Use systemd instead of rc.local, cron @reboot, or manual launches for better control.


πŸ› οΈ Create a Custom systemd Service

Let’s automate a Python GPIO script using systemd.

βœ… Step 1: Create a Script

/home/pi/blink.py

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

while True:
    GPIO.output(17, True)
    time.sleep(1)
    GPIO.output(17, False)
    time.sleep(1)

Make executable:

chmod +x /home/pi/blink.py

βœ… Step 2: Create a systemd Service File

/etc/systemd/system/blink.service

[Unit]
Description=Blink LED on GPIO17
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/blink.py
Restart=always
User=pi
WorkingDirectory=/home/pi
StandardOutput=inherit
StandardError=inherit

[Install]
WantedBy=multi-user.target

βœ… Make sure the path to Python (/usr/bin/python3) is correct.


βœ… Step 3: Enable & Start Service

sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable blink.service
sudo systemctl start blink.service

Check status:

systemctl status blink.service

βœ… Your Python script now runs on boot and restarts if it crashes.


πŸ” Manage Services with systemctl

πŸ”§ CommandπŸ“ Description
sudo systemctl start nameStart service immediately
sudo systemctl stop nameStop the running service
sudo systemctl enable nameEnable service at boot
sudo systemctl disable nameDisable service from autostart
sudo systemctl restart nameRestart the service
sudo systemctl status nameView current service status
journalctl -u nameView service logs

πŸ§ͺ Use Cases for systemd on Raspberry Pi

βš™οΈ Use CaseπŸ”§ Description
Auto-start Python GPIO appRun LED/sensor/automation scripts at boot
Node.js/MQTT serverKeep backend APIs alive
Data logging with sensorsEnsure sensors write to CSV/DB on restart
Motion detection cameraKeep libcamera or OpenCV apps active
Network monitorRestart network tools or VPNs if they fail

πŸ” Run Services with Root or pi User?

  • Use User=pi to run as a normal user (safe for most scripts)
  • Use User=root only for scripts that require root permissions (e.g., accessing /sys, GPIO via device tree)

βœ… Match file/folder permissions accordingly.


πŸ’‘ Debugging systemd Services

🧠 TipπŸ’¬ Explanation
Use journalctl -u <service>Check stdout/stderr and crash messages
Add Restart=on-failureService restarts only on crash
Use WorkingDirectoryPrevent script from failing due to path issues
Check permissionsLogs fail if script owner has no execute rights

πŸ§ͺ Bonus: Restart Service on File Change (Optional)

For dynamic development environments:

[Service]
ExecStart=/usr/bin/python3 /home/pi/dev_script.py
Restart=on-failure
WatchdogSec=5

βœ… Use inotify or advanced systemd.path units for full file monitoring.


πŸ“Œ Summary – Recap & Next Steps

systemd gives you superior control, auto-start, logging, and error handling for your Raspberry Pi scripts and services. It’s the professional way to deploy long-running apps.

πŸ” Key takeaways:

  • systemd is better than cron @reboot or rc.local for service management
  • Scripts become reliable, restartable system services
  • Full boot/startup integration and log visibility
  • Supports GPIO, Python, Node.js, sensor apps, and network tools

βš™οΈ Real-world relevance: Ideal for IoT services, GPIO automation, dashboards, and roboticsβ€”ensuring 24/7 stability.


❓ FAQs – Raspberry Pi Services & systemd

❓ What’s the difference between cron and systemd?

βœ… cron is for scheduling tasks; systemd is for managing persistent background services that start at boot or restart automatically.


❓ Can I run a GUI app as a service?

βœ… Technically yes, but GUI apps should be run under the graphical target with a display server (advanced).


❓ How do I debug if a systemd service won’t start?

βœ… Use:

sudo journalctl -xe
sudo systemctl status yourservice.service

❓ What permissions does a systemd service need?

βœ… Depends on the user specified in [Service] β†’ User=pi/root. Scripts must have correct ownership and be executable.


❓ Can I run multiple services from different scripts?

βœ… Yes. Create separate .service files for each and manage them individually or with a .target unit.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ” Raspberry Pi – Services & systemd Usage

Or Copy Link

CONTENTS
Scroll to Top