Subnet Toolsand other network tips

Python as a service

I use Raspberry Pi for all sorts of little jobs and automation and for this Python is the natural choice. It’s often useful to have your script running as a service and able to automatically start on boot. It’s quite easy to set up a new service using systemd on a Raspberry Pi running Raspbian.

Start by creating the service Unit file.

sudo nano /lib/systemd/system/{name of service}.service

In this file put the following:

[Unit]
Description={Description of service}
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /{/path/to/service.py}
[Install]
WantedBy=multi-user.target

This file will need permissions set appropriately.

sudo chmod 644 /lib/systemd/system/{name of service}.service

You can then enable and start your service

sudo systemctl daemon-reload
sudo systemctl enable {name of service}.service
sudo systemctl start {name of service}.service

Of course your script should be formatted to run as a continuous loop – while 1: should suffice in most cases.

Comments are currently closed.