Keeping a Service Running with systemd


Recently, I created a simple Python server to control a Sky Q box and an LG WebOS television programmatically. The goal was to build a straightforward interface for someone who finds modern technology overwhelming. The kind of person who would struggle with navigating smart TV apps, juggling multiple remotes, or dealing with unreliable commercial software. I wanted to remove as many barriers as possible and make the experience as seamless as it could be.
One concern I had early on was reliability. The server was running precariously on a Raspberry Pi: if the Pi rebooted, the server wouldn't start automatically. If the server crashed for any reason, it would stay down until someone manually restarted it. For the person I was designing this for, it was absolutely essential that the system "just worked." If it became unreliable, there was a real risk they would lose faith, and with it their willingness to engage with the system at all.
Today, I implemented a proper solution using systemd, the standard service manager for Linux systems. Systemd allowed me to define my Python server as a service that automatically starts at boot, monitors itself, and restarts if it crashes unexpectedly. This small change will hopefully improve the overall reliability of the system.
How to set it up
Create a
systemd
service file (example:/etc/systemd/system/yourservicename.service
):[Unit] Description=My Python Application After=network.target # Wait for network to load [Service] ExecStart=/path/to/venv/bin/python /path/to/your_app.py WorkingDirectory=/path/to/working/directory # ensures relative file paths work correctly. Restart=always # Always restart if the server crashes RestartSec=5 # Wait 5s before restarting the server User=pi [Install] WantedBy=multi-user.target # Start the service when we reach a fully booted system
Enable and start it:
sudo systemctl daemon-reload sudo systemctl enable yourservicename.service sudo systemctl start yourservicename.service
Check its status:
sudo systemctl status tv_control.service
Final thoughts
By setting up the Python server as a systemd service, I've taken an important step toward ensuring that the system remains dependable and frictionless for the person using it. They shouldn’t have to think about whether the server is running or worry about fixing anything if something goes wrong, it should just work, quietly and reliably in the background. This approach not only improves technical stability but also builds trust, making it far more likely that the system will become a comfortable and lasting part of their daily routine.
Subscribe to my newsletter
Read articles from Gregor Soutar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Gregor Soutar
Gregor Soutar
Software engineer at the UK Astronomy Technology Centre, currently developing instrument control software for MOONS, a next-generation spectrograph for the Very Large Telescope (VLT).