1

i've created a py file to monitor my home network. The script it self is running ok. I've created a service with systemd that will run this script every 30 sec and copy it in /etc/systemd/system/.

This is the service file:

[Unit]
Description=Monitor Network Connection
After=multi-user.target network-online.target networking.service

[Service] Type=idle WorkingDirectory=/home/pi ExecStartPRE=bin/sleep 3 ExecStart=/usr/bin/python /home/pi/BoApp/monitor_net.py Restart=on-abort

[Timer] OnBootSec=30 OnUnitActiveSec=30

[Install] WantedBy=multi-user.target WantedBy=timers.target

Now, when i manually start the service and do a status check, i get:

 monitor_net.service - Monitor Network Connection
   Loaded: loaded (/etc/systemd/system/monitor_net.service; enabled; vendor preset: enabled)
   **Active: inactive (dead)** since Sun 2020-10-11 11:28:21 EEST; 1min 28s ago
  Process: 1699 ExecStart=/usr/bin/env python3 /home/pi/BoApp/monitor_net.py (code=exited, status=0/SUCCESS)
 Main PID: 1699 (code=exited, status=0/SUCCESS)

Basically it only runs the py script once and then stop.

Can you help me here to understand why this service is inactive ?

Many thanks!

Ingo
  • 42,097
  • 20
  • 85
  • 197
Bogdan
  • 11
  • 1
  • 2
  • This is not a Raspberry Pi question, per se. I'd suggest reposting it on the Unix & Linux Stack Exchange, where it will reach a wider audience. – patbarron Oct 11 '20 at 18:05

1 Answers1

1

It seems you have merged a timer unit with a service unit. The service file you are showing contains a [Timer] section but a service unit does not know such a section. You have to use a separate timer unit. Have a look at man systemd.timer.

There are other issues with your service unit. The Type=idle is doing nothing useful, so just omit it. I do not understand for what to use ExecStartPre=bin/sleep 3. With proper dependencies it is not necessary to wait three seconds to continue. Also Restart=on-abort doesn't make much sense because the service will start again anyway every 30 seconds. The restart option may only confuse systemd.

A timer unit with the same name as the service unit will start the service. It does not need an [Install] section because it is started by the timer. So I suggest to try this units, but first delete the old service.

rpi ~$ sudo rm /etc/systemd/system/your-old.service

Then create a timer unit with:

rpi ~$ sudo systemctl edit --force --full network-monitor.timer

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

[Unit]
Description=Run network monitor every 30 seconds

[Timer] OnBootSec=30 OnUnitActiveSec=30

[Install] WantedBy=timers.target

Next create the corresponding service unit. Be sure to use the same name as the timer unit, e.g. network-monitor:

rpi ~$ sudo systemctl edit --force --full network-monitor.service

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

[Unit]
Description=Monitor Network Connection
Wants=network.target
After=network.target
# If it doesn't work, try
#After=network-online.target networking.service
#Wants=network-online.target networking.service

[Service] Type=oneshot WorkingDirectory=/home/pi ExecStart=/usr/bin/python /home/pi/BoApp/monitor_net.py

By the way, you should really use python3 now instead of unsupported python2 (called with /usr/bin/python).

Ingo
  • 42,097
  • 20
  • 85
  • 197