0

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?

  • 1
    Please edit the question and add more information, e.g. the file location. – joan Mar 19 at 13:11
  • put the unit file in either /lib/systemd/system/ or /etc/systemd/system/ – oh.dae.su Mar 19 at 15:33
  • I cannot do it since I have "no permission" – xoani Mar 20 at 7:39
2

I know nothing about your script, if it runs only one time on boot up or stays always in the background as a service, if it needs root rights, if it produces text or graphical output, to name the most important one. But for a first try you can start with a generic unit file that can be modified to your needs.

Create a new service with:

rpi ~$ sudo systemctl --force --full edit hs110.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=hs110 data collection
After=multi-user.target

[Service]
User=pi
WorkingDirectory=/home/pi
ExecStart=/usr/bin/python3 /home/pi/hs110-data-collect.py

[Install]
WantedBy=multi-user.target

Then enable the service and reboot:

rpi ~$ sudo systemctl enable hs110.service
rpi ~$ sudo systemctl reboot

Look at its status with:

rpi ~$ systemctl status hs110.service

As you told in a comment the script will do text output only on the console. If it is running as service you will find this output in the systemd journal:

rpi ~$ journalctl --boot --unit=hs110.service
  • Hi Ingo, thanks for your help. I did what you said and I am adding the message I got to my original question now. – xoani Mar 21 at 8:33
  • @xoani exec code 203 means "no such file or directory". Seems your python script isn't set executable. Change the line ExecStart=. I have updated the answer. Please just replace the status message in your question. Also login as user pi and execute python3 /home/pi/hs110-data-collect.py. Does it work? Do you have some output? If so, is it just text on the console? Does the script runs only one time and terminates, or is it running all the time to collect data continuously? – Ingo Mar 21 at 10:29
  • I have updated the message. It seems like the script is running on boot now (it worked before, just not on boot). When I start the script manually, the data is collected every 5 seconds and I see that in the RPi terminal. Now this message shows that the data got collected once at 12:06:45 but it doesn't show that it gets collected every 5 seconds. Is this correct? (Just to clarify, the "no data returned on power request" is the right message at the moment, because I don't have any data yet, so this is what the output should be) – xoani Mar 21 at 11:15
  • @xoani OK, your script is running now as service. I cannot say anything about its execution because I don't know something about it. If it outputs every 5 seconds that it has collected data then you should find this message with journalctl. I have updated the answer at the end. But a logging message every 5 sec will fill your journal fast and make it a huge file. It is up to you to optimize the script. It would be nice if you could accept my answer with a tick at the tick on the left side. This will finish the question and show others that it has a solution. – Ingo Mar 21 at 11:41
  • Hello, I accepted the answer, thanks for the heads up and for all your help! The script runs in the background but I am confused because in the journal the times are not every 5 seconds (there are a lot of messages at 12:20:29). But when I run the script manually, I do get a message in return every 5 seconds. Do you have any idea why? – xoani Mar 21 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.