Skip to content

Latest commit

 

History

History
77 lines (54 loc) · 1.28 KB

File metadata and controls

77 lines (54 loc) · 1.28 KB

Run Python script as a service

Start your Python script with a 'portable' shebang (https://realpython.com/python-shebang/)

#!/usr/bin/env python3

and make sure it can be executed

chmod +x /home/pi/some-service.py

Head over to the folder containing service definitions.

cd /lib/systemd/system/

Create a new service definition file using elevated privileges.

sudo vi some.service

Add some content explained here:

[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.service

Then activate the service.

sudo systemctl daemon-reload
sudo systemctl enable some.service
sudo systemctl start some.service

Check the status with

sudo systemctl status some.service

or watch the logs

sudo journalctl -f -u some.service

and stop it when done ;-)

sudo systemctl start some.service