I have a Raspberry Pi running Debian Jessie.
The Pi is acting as a VPN gateway for when I travel.
I have a monitor script – python – which, when triggered, generates a list of connected clients and sends that list via MQTT to my network monitor machine. This all works perfectly. Every time I launch the script, it runs and I can see the client list 'at the other end' of the MQTT chain. The client list includes a timestamp from the VPN machine which shows at what time it was triggered to give the report.
I have additionally configured a Systemd service file so I can automatically trigger this script. This is my service file – 'vpnmon.service':
[Unit] Description=VPN Monitor script [Service] Type=simple ExecStart=/usr/bin/python /home/nick/lib/checkusers.py
This is triggered by a systemd timer 'vpnmon.timer':
[Unit] Description=Runs vpnmon script every five seconds [Timer] OnBootSec=10s Unit=vpnmon.service OnUnitInactiveSec=5s [Install] WantedBy=multi-user.target
This does all 'work'.
BUT the script is seemingly not triggered at regular time intervals. Sometimes 10 seconds, sometimes 15 sometimes 20 – sometimes a whole minute elapses before it is triggered.
So I type the following to query the service status:
systemctl status vpnmon.service
I get a good result:
● vpnmon.service - VPN Monitor script Loaded: loaded (/lib/systemd/system/vpnmon.service; static) Active: inactive (dead) since Thu 2017-09-28 09:52:01 UTC; 1s ago Process: 1138 ExecStart=/usr/bin/python /home/nick/lib/checkusers.py (code=exi ted, status=0/SUCCESS) Main PID: 1138 (code=exited, status=0/SUCCESS)
In the course of doing this I noticed a weird thing. If I repeatedly query the status, I constantly get good results, and I notice that the very act of querying the service status via command line like this causes the script to be triggered exactly when expected i.e. at 5 second intervals…
What is there in the act of querying a systemd service status could cause the apparent intermittant triggering to disappear??
Is this something akin to Python 'buffering; when you use a script to write stuff to a file??
Best Answer
Raman - thank you! Nailed it.
I added the following to vpnmon.timer:
It works perfectly now.
Details of that setting are here:
https://www.freedesktop.org/software/systemd/man/systemd.timer.html#AccuracySec=
Thank you.