How to create linux service

Table of Contents
  1. Create a file with service script
sudo nano /lib/systemd/system/sample.service
  1. 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
  1. 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
  1. 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