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.
- create a service file using the following command:
sudo nano /etc/systemd/system/watermonitor.service
- 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
- Enable the service file by typing the following commands:
sudo systemctl daemon-reload
sudo systemctl enable watermonitor.service
- test the service file by manually starting it:
sudo systemctl start watermonitor.service
- monitoring the terminal of the file:
sudo systemctl status watermonitor.service
- stop service:
sudo systemctl stop watermonitor.service
- restart the service:
sudo systemctl restart watermonitor.service
- disable the service:
sudo systemctl disable watermonitor.service