Linux Service Auto‐Start Script - Digital-Waters/WaterMonitorDevice GitHub Wiki

The following instructions allow the mainloop to start at the start-up of the raspberryPI.

  1. create a service file using the following command:
sudo nano /etc/systemd/system/watermonitor.service
  1. write the following configuration details to the service file. This requires the path of the mainloop.py file, this can be obtained by typing readlink -f mainloop.py in the working directory:
[Unit]
Description=My Python Script
After=network.target

[Service]
ExecStart=/usr/bin/python3 [path to mainloop.py]
WorkingDirectory=[path to the directory that main.py is in]
StandardOutput=inherit
StandardError=inherit
Restart=always
User=rpi

[Install]
WantedBy=multi-user.target

next ctrl-s to save and ctrl-x to exit

  1. Enable the service file by typing the following commands:
sudo systemctl daemon-reload
sudo systemctl enable watermonitor.service
  1. test the service file by manually starting it:
sudo systemctl start watermonitor.service
  1. monitoring the terminal of the file:
sudo systemctl status watermonitor.service
  1. stop service:
sudo systemctl stop watermonitor.service
  1. restart the service:
sudo systemctl restart watermonitor.service
  1. disable the service:
sudo systemctl disable watermonitor.service