I have a Python script (that works) set up and I want to run it automatically every time the Raspberry Pi is on.
I have read that I need to create a unit file and I have done so, but whenever I try these commands:
sudo systemctl enable myscript.service
sudo systemctl start myscript.service
sudo systemctl stop myscript.service
I get the following error:
Failed to start myfile.service. Unit myfile.service not found.
I have no idea if the problem is that I am saving the file and the script in the wrong places or if I am missing something else. Help?
Both hs110-data-collect-py (the python script) and the unit file hs110.service are saved in: /home/pi/hs110
**************************** UPDATE
After doing what Ingo suggested, I get this message:
* hs110.service - hs110 data collection
Loaded: loaded (/etc/systemd/system/hs110.service; enabled; vendor preset: en
Active: active (running) since Thu 2019-03-21 12:06:43 CET; 1min 6s ago
Main PID: 828 (python3)
CGroup: /system.slice/hs110.service
`-828 /usr/bin/python3 /home/pi/hs110-data-collect.py
Mar 21 12:06:43 raspberrypi systemd[1]: Started hs110 data collection.
Mar 21 12:06:45 raspberrypi python3[828]: Socket closed.
Mar 21 12:06:45 raspberrypi python3[828]: No data returned on power request.
lines 1-10/10 (END)
************************UPDATE
When I use journalctl --boot --unit=hs110.service
to see the output of the script in the terminal, I get this message:
-- Logs begin at Thu 2016-11-03 18:16:43 CET, end at Thu 2019-03-21 12:35:01 CET. --
Mar 21 12:06:43 raspberrypi systemd[1]: Started hs110 data collection.
Mar 21 12:06:45 raspberrypi python3[828]: Socket closed.
Mar 21 12:06:45 raspberrypi python3[828]: No data returned on power request.
Mar 21 12:20:29 raspberrypi python3[828]: Socket closed.
Mar 21 12:20:29 raspberrypi python3[828]: No data returned on power request.
Mar 21 12:20:29 raspberrypi python3[828]: Socket closed.
Mar 21 12:20:29 raspberrypi python3[828]: No data returned on power request.
Mar 21 12:20:29 raspberrypi python3[828]: Socket closed.
Mar 21 12:20:29 raspberrypi python3[828]: No data returned on power request.
Mar 21 12:20:29 raspberrypi python3[828]: Socket closed.
Mar 21 12:20:29 raspberrypi python3[828]: No data returned on power request.
Mar 21 12:20:29 raspberrypi python3[828]: Socket closed.
Mar 21 12:20:29 raspberrypi python3[828]: No data returned on power request.
Mar 21 12:20:29 raspberrypi python3[828]: Socket closed.
Mar 21 12:20:29 raspberrypi python3[828]: No data returned on power request.
Mar 21 12:20:29 raspberrypi python3[828]: Socket closed.
Mar 21 12:20:29 raspberrypi python3[828]: No data returned on power request.
Mar 21 12:20:29 raspberrypi python3[828]: Socket closed.
Mar 21 12:20:29 raspberrypi python3[828]: No data returned on power request.
Mar 21 12:20:29 raspberrypi python3[828]: Socket closed.
Mar 21 12:20:29 raspberrypi python3[828]: No data returned on power request.
The problem is that I should get this "socket closed" message every 5 seconds (this happens when I just write python3 hs110-data-collect.py
). So why don't I get a return message every 5 seconds in the journal?