Start your Python script with a 'portable' shebang (https://realpython.com/python-shebang/)
#!/usr/bin/env python3and make sure it can be executed
chmod +x /home/pi/some-service.pyHead over to the folder containing service definitions.
cd /lib/systemd/system/Create a new service definition file using elevated privileges.
sudo vi some.serviceAdd some content explained here:
- https://wiki.archlinux.org/title/Systemd
- https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files
- https://coreos.com/os/docs/latest/getting-started-with-systemd.html
[Unit]
Description=Some Service
After=multi-user.target
[Service]
Type=simple
ExecStart=/home/pi/some-service.py
Restart=on-abort
[Install]
WantedBy=multi-user.target
and change the access to the file
sudo chmod 644 /lib/systemd/system/some.serviceThen activate the service.
sudo systemctl daemon-reload
sudo systemctl enable some.service
sudo systemctl start some.serviceCheck the status with
sudo systemctl status some.serviceor watch the logs
sudo journalctl -f -u some.serviceand stop it when done ;-)
sudo systemctl start some.service