How to create linux service
Table of Contents
- Create a file with service script
sudo nano /lib/systemd/system/sample.service
- Fill it with the next content
[Unit]
Description=My Sample Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/sample.py > /home/pi/sample.log 2>&1
[Install]
WantedBy=multi-user.target
- Assign rights
[Unit]
Description=My Sample Service
After=multi-user.target
[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/sample.py > /home/pi/sample.log 2>&1
User=pi
[Install]
sudo chmod 644 /lib/systemd/system/sample.service
- Enable it
sudo systemctl daemon-reload
sudo systemctl enable sample.service
sudo reboot
Useful commands
Check status
service sample status
Check who is running the service
systemctl show -pUser,UID service-name