0

I've made a very simple python program which extract the latests news popped in some website and send them to me via Telegram. The program is working perfectly when I am launching the command below in the console :

/usr/bin/python3.9 /home/dietpi/news/news.py

However when I try to automate it in (to automatically restart if there is any bug or so), I noticed the services is blocked into the ExecStartPre step forever :

[Unit]
Description=News Service
Wants=network.target
After=network.target

[Service]
ExecStartPre=/bin/sleep 10
ExecStart=/usr/bin/python3.9 /home/dietpi/news/news.py
Restart=always

[Install]
WantedBy=multi-user.target

I put ExecStartPre command to let the Pi to setup properly the network before launching the program (I noticed a failure occur if not done, as the program starts too quickly and generates an error).

When I reboot the Pi, here is what I can see when I am opening the status of the services (by using the command: systemctl --type=service):

UNIT         LOAD   ACTIVE     SUB       JOB   DESCRIPTION
news.service loaded activating start-pre start News Service

When I look more into detail on this service here is what I have (by using command: sudo systemctl status news.service):

● news.service - News Service
     Loaded: loaded (/etc/systemd/system/news.service; enabled; vendor preset: enabled)
     Active: activating (start-pre) since Fri 2022-02-04 17:03:58 GMT; 2s ago
Cntrl PID: 552 (sleep)
      Tasks: 1 (limit: 4915)
        CPU: 4ms
     CGroup: /system.slice/news.service
             └─552 /bin/sleep 10

Feb 04 17:03:58 DietPi systemd[1]: Starting News Service...

If I launch this command multiple time, I see the "activating" step goes up to 10s, then starts again from 0s >>> Which shows I am stuck in the ExecStartPre step :(

If you have any idea on how to solve this issue, it would be much appreciated :)

Maxence Hermand
  • 105
  • 1
  • 3
  • 12
  • `I see the "activating" step goes up to 10s, then starts again from 0s >>> Which shows I am stuck in the ExecStartPre step` How are you monitoring this? Do you see specific this in log? How did you end on this specific conclusion? – KamilCuk Feb 04 '22 at 17:45
  • I launched the command *systemctl --type=service* several time to see how it evolved, then noticed when the "start-pre" step had was launched more than 9s ago, it starting again from 0s... Which lead me to the conclusion that : * Something might happen with the ExecStart which lead to a crash and restart of the service... – Maxence Hermand Feb 04 '22 at 18:10
  • `several time` how do you know you just were not lucky? Check the system logs. `Something might happen with the ExecStart` Yes. So check the system logs and find out. You have `Restart=always` so it's always going to be restarting. – KamilCuk Feb 04 '22 at 20:30

1 Answers1

0

Try to create your own sleep script:

sleep.py:

import time
import sys

if __name__ == '__main__':
    time.sleep(10)
    sys.exit()

In your system unit:

ExecStartPre /usr/bin/python3.9 /home/dietpi/news/sleep.py

Personally, I prefer use supervisord to launch my python script as a service:

/etc/supervisor/conf.d/news.conf:

[program:news]
command = /usr/bin/python3.9 news.py
directory = /home/dietpi/news/
user = dietpi
autostart = true
autorestart = true
stdout_logfile = /var/log/supervisor/news.log
redirect_stderr = true
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • I've tried creating my own sleep script as you suggested... The situation stays the same. Rather something block the service to go from *ExecStartPre* to *ExecStart* Or something goes wrong with my python script, which seems strange as it works perfectly from the console. I'll try with supervisor to see how it goes. Why do you personally use supervisord? Is there any way to offset the launch of the service with supervisord (by 10 sec like in systemd for example)? – Maxence Hermand Feb 04 '22 at 18:24