Systemd service doesn't work corectly

Hi,
I searched about my problem but didn’t find a solution.
I am trying to monitor the RPi running OSMC with the script from GitHub - Sennevds/system_sensors: Logging of system sensor specific for the RPI and sending them to a MQTT broker

The problem is the script send the output just once even the service is running.

Here is the service:

[Unit]
Description=System Sensor service
After=multi-user.target

[Service]
User=osmc
Group=osmc
Type=simple
WorkingDirectory=/home/osmc/system_sensors/src/
ExecStart=/usr/bin/python3 /home/osmc/system_sensors/src/system_sensors.py /home/osmc/system_sensors/src/settings.yaml

Restart=always

[Install]
WantedBy=multi-user.target

Here is the service status:

osmc@osmc:~$ sudo systemctl status system-sensors.service

  • system-sensors.service - System Sensor service
    Loaded: loaded (/lib/systemd/system/system-sensors.service; enabled; vendor preset: enabled)
    Active: active (running) since Thu 2020-02-13 21:21:51 EET; 50min ago
    Main PID: 950 (python3)
    CGroup: /system.slice/system-sensors.service
    `-950 /usr/bin/python3 /home/osmc/system_sensors/src/system_sensors.py /home/osmc/system_sensors/src/settings.yaml

Feb 13 21:21:51 osmc systemd[1]: Started System Sensor service.
osmc@osmc:~$

Any idea what could be?

Thanks

  1. How does it perform if you run the job from the command line?

  2. The github page shows a different service unit. Why did you change it?

  1. it is working, as should be. sorry, i forgot to mention this
  2. initially i started with the one from github, but i change it trying to make it work. BTW, w/o WorkingDirectory doesn’t work at all (service starts but no result)

Thanks

Can you provide enough information for me to replicate your set-up, in particular configuration files. Are you using Mosquitto on the Pi?

I tried again different settings w/o success:

  • move the system-sensors.service in /etc/systemd/system/
  • install python 3.7 and launch everything using it
  • different settings in system-sensors.service with daemon reload
    Every time I get the data when running the script manually but not when the service is running. This is the reason i believe it is not a problem with the script but with the way the service is running.

Here is my setup:

  • 1 RPi hosting dedicated Mosquitto broker and Home Assistant; the configuration is working as i have different sensors using this broker and seen in HA
  • 1 RPi hosting OSMC (latest updates applied) on which I am trying to run this script; the idea is to monitor it by Home Assistant

What configuration file should I share?

I would like to be able to run just the system_sensors script and save whatever data it produces to a Mosquitto broker that will be running on the same device. I assume that I won’t need to have anything to do with Home Assistant.

Those that are used for the system_sensors script and Mosquitto broker.

Thank you for involvement! I am about to abandon this because I lost too much time w/o results.
Coming to the issue, I don’t understand why Mosquitto broker config is needed. As I mentioned, this is on another RPi and the messages reach it when I run the same command I used inside service. In my view, this show that everything is fine with MQTT.

Here is the settings.yaml:

mqtt:
  hostname: 192.168.x.y
  port: 1883 #defaults to 1883
  user: edited username
  password: edited password
deviceName: osmc
client_id: osmc
timezone: Europe
power_integer_state: false #defaults to false
update_interval: 60 #Defaults to 60
check_wifi_strength: false

I didn’t modify anything on the python script. it is the same as system_sensors/src/system_sensors.py at master · Sennevds/system_sensors · GitHub

Regarding the service, now (I did many changes:frowning_face:) it is looking as below:

[Unit]
Description=OSMC System Sensor service
After=multi-user.target

[Service]
User=osmc
Type=simple
WorkingDirectory=/usr/local/lib/system_sensors/src/
ExecStart=/usr/bin/python3 /usr/local/lib/system_sensors/src/system_sensors.py /usr/local/lib/system_sensors/src/settings.yaml

Restart=always

[Install]
WantedBy=multi-user.target

osmc@osmc:~$ ls -la /etc/systemd/system/system-sensors.service
-rw-r–r-- 1 root root 336 Feb 15 20:38 /etc/systemd/system/system-sensors.service

When I am running below command everything is reported as expected (I see the messages when subscribing to Mosquitto broker and the sensors are updated in Home Assistant).

/usr/bin/python3 /usr/local/lib/system_sensors/src/system_sensors.py /usr/local/lib/system_sensors/src/settings.yaml

I reformatted your settings.yaml for clarity. Using the following settings.yaml myself:

mqtt:
  hostname: localhost
  port: 1883 #defaults to 1883
deviceName: osmc
client_id: osmc
timezone: Europe/London
power_integer_state: false #defaults to false
update_interval: 60 #Defaults to 60
check_wifi_strength: false

what mosquitto_sub command should I use to view the data?

Edit: It seems that mosquitto_sub -t \# -h localhost does the job.

You need to edit system_sensors.py and change line 97 so that vcgencmd becomes /opt/vc/bin/vcgencmd.

The underlying reason is that the programmer assumed that vcgencmd would be in the path (in /usr/bin), as it is in Raspbian. Unfortunately, Sam decided to put it in /opt/vc/bin. I’ve no idea why it’s been moved, but that was causing the script to fail.

Thank you! It is working.
Can you tell me how did you discover this? I was sure it was something related to systemd because was running as command.

My initial thinking was that it was related to the environment. Unfortunately, I got a bit side-tracked by the script’s “sub-optimal” signal handling, that you’ve probably noticed yourself, and that I thought might be related to your problem.

Once back on track, I tried to run it from cron – and it failed, as well, That pretty well confirmed that it was an environment issue, so I ran this command to create a clean environment, without the osmc user’s profile:

env -i /bin/bash --noprofile --norc

followed by

/usr/bin/python3 /home/osmc/system_sensors/src/system_sensors.py /home/osmc/system_sensors/src/settings.yaml

That revealed the error. Fortunately, it was the only show-stopper I found, though, as I mentioned, the signal handling, eg kill and Ctrl-C, isn’t working properly – but you can live with that.