Thuta Learning
AdvancedHardwarebeginner

Running Services on Boot (systemd)

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand Running Services on Boot (systemd) without any of the intimidation
  • Be able to run the hardware wiring/code yourself
  • Apply this concept right away in a real project

Let's think about it this way for a moment

It's inconvenient to have to manually re-run a Python script (LED control, sensor monitoring) every time there's a power failure or restart — register it as a systemd service instead, following the same systemctl enable pattern from the Linux tutorial, and the script will run automatically every time the Pi boots. You create a service file (.service) in /etc/systemd/system/ and configure fields like ExecStart (the command to run), WorkingDirectory, and User.

Let's connect it to a real scenario

Create a my-project.service file, write ExecStart=/usr/bin/python3 /home/pi/my-project/main.py in the [Service] section, and run sudo systemctl enable my-project.service (from the Linux tutorial) to make it auto-start on every boot — you can check whether it's running with sudo systemctl status my-project.service (the same systemctl status pattern from the Linux tutorial).

Let's look at it together

text
# /etc/systemd/system/my-project.service
[Unit]
Description=My Raspberry Pi Project
After=network.target

[Service]
ExecStart=/usr/bin/python3 /home/pi/my-project/main.py
WorkingDirectory=/home/pi/my-project
User=pi
Restart=always

[Install]
WantedBy=multi-user.target

# Then:
# sudo systemctl enable my-project.service
# sudo systemctl start my-project.service
You should see
Running sudo systemctl status my-project.service should confirm the service is running with 'active (running)' — and the script should keep auto-running even after restarting the Pi.

5-minute try-it

Revisit the systemd lesson from the Linux tutorial and write a service file yourself for your own Python script (if you have one).

A quick word of caution

Using Restart=always while your script still has a bug can trigger a crash-restart loop (it keeps crashing and restarting endlessly) — confirm the script runs stably by hand before registering it as a service.

Easy traps

  • Writing just 'python3' in ExecStart instead of the full path (/usr/bin/python3), so systemd's environment can't find the command
  • Leaving out Restart=always, so the script doesn't auto-restart if it crashes

Now try it yourself

Revisit the systemd lesson from the Linux tutorial and write a service file yourself for your own Python script (if you have one).

You'll know it worked when: Running sudo systemctl status my-project.service should confirm the service is running with 'active (running)' — and the script should keep auto-running even after restarting the Pi.

Running Services on Boot (systemd) | Thuta Learning