Sennevds / system_sensors

Logging of system sensor specific for the RPI and sending them to a MQTT broker
MIT License
367 stars 113 forks source link

RPi 4 - systemd doesn't work properly #80

Closed HDmaniac closed 3 years ago

HDmaniac commented 3 years ago

I can't get anything to work unless I manually start the script (python3 src/system_sensors.py src/settings.yaml)

I've tried reinstalling the requirements because it mentioned rpi_bad_power so I thought it may be a dependancy issue.

Here's what happens as well as my troubleshooting efforts:

pi@raspberrypi:~/system_sensors $ sudo systemctl status system_sensors.service
● system_sensors.service - System Sensor service
   Loaded: loaded (/etc/systemd/system/system_sensors.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2021-03-14 23:23:27 CET; 3s ago
  Process: 2580 ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sensors.py /home/pi/system_sensors/src/settings.yaml (code=exited, status=1/FAILURE)
 Main PID: 2580 (code=exited, status=1/FAILURE)

Mar 14 23:23:27 raspberrypi systemd[1]: Started System Sensor service.
Mar 14 23:23:27 raspberrypi python3[2580]: Traceback (most recent call last):
Mar 14 23:23:27 raspberrypi python3[2580]:   File "/home/pi/system_sensors/src/system_sensors.py", line 13, in <module>
Mar 14 23:23:27 raspberrypi python3[2580]:     from rpi_bad_power import new_under_voltage
Mar 14 23:23:27 raspberrypi python3[2580]: ModuleNotFoundError: No module named 'rpi_bad_power'
Mar 14 23:23:27 raspberrypi systemd[1]: system_sensors.service: Main process exited, code=exited, status=1/FAILURE
Mar 14 23:23:27 raspberrypi systemd[1]: system_sensors.service: Failed with result 'exit-code'.
pi@raspberrypi:~/system_sensors $ pip3 install --upgrade --force-reinstall -r requirements.txt
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting paho-mqtt==1.5.0
  Using cached https://www.piwheels.org/simple/paho-mqtt/paho_mqtt-1.5.0-py3-none-any.whl (61 kB)
Collecting psutil==5.6.6
  Using cached psutil-5.6.6.tar.gz (447 kB)
Collecting pytz==2019.2
  Downloading https://www.piwheels.org/simple/pytz/pytz-2019.2-py2.py3-none-any.whl (508 kB)
     |████████████████████████████████| 508 kB 66 kB/s
Collecting PyYAML==5.2
  Using cached PyYAML-5.2.tar.gz (265 kB)
Collecting rpi_bad_power==0.0.3
  Downloading https://www.piwheels.org/simple/rpi-bad-power/rpi_bad_power-0.0.3-py3-none-any.whl (3.0 kB)
Using legacy 'setup.py install' for psutil, since package 'wheel' is not installed.
Using legacy 'setup.py install' for PyYAML, since package 'wheel' is not installed.
Installing collected packages: rpi-bad-power, PyYAML, pytz, psutil, paho-mqtt
  Attempting uninstall: rpi-bad-power
    Found existing installation: rpi-bad-power 0.0.3
    Uninstalling rpi-bad-power-0.0.3:
      Successfully uninstalled rpi-bad-power-0.0.3
  Attempting uninstall: PyYAML
    Found existing installation: PyYAML 5.2
    Uninstalling PyYAML-5.2:
      Successfully uninstalled PyYAML-5.2
    Running setup.py install for PyYAML ... done
  Attempting uninstall: pytz
    Found existing installation: pytz 2019.2
    Uninstalling pytz-2019.2:
      Successfully uninstalled pytz-2019.2
  Attempting uninstall: psutil
    Found existing installation: psutil 5.6.6
    Uninstalling psutil-5.6.6:
      Successfully uninstalled psutil-5.6.6
    Running setup.py install for psutil ... done
  Attempting uninstall: paho-mqtt
    Found existing installation: paho-mqtt 1.5.0
    Uninstalling paho-mqtt-1.5.0:
      Successfully uninstalled paho-mqtt-1.5.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
homeassistant 2021.2.3 requires pip<20.3,>=8.0.3, but you have pip 21.0.1 which is incompatible.
homeassistant 2021.2.3 requires pytz>=2020.5, but you have pytz 2019.2 which is incompatible.
homeassistant 2021.2.3 requires pyyaml==5.4.1, but you have pyyaml 5.2 which is incompatible.
Successfully installed PyYAML-5.2 paho-mqtt-1.5.0 psutil-5.6.6 pytz-2019.2 rpi-bad-power-0.0.3
pi@raspberrypi:~/system_sensors $ sudo systemctl restart system_sensors.service
pi@raspberrypi:~/system_sensors $ sudo systemctl status system_sensors.service
● system_sensors.service - System Sensor service
   Loaded: loaded (/etc/systemd/system/system_sensors.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2021-03-14 23:23:27 CET; 3s ago
  Process: 2580 ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sensors.py /home/pi/system_sensors/src/settings.yaml (code=exited, status=1/FAILURE)
 Main PID: 2580 (code=exited, status=1/FAILURE)

Mar 14 23:23:27 raspberrypi systemd[1]: Started System Sensor service.
Mar 14 23:23:27 raspberrypi python3[2580]: Traceback (most recent call last):
Mar 14 23:23:27 raspberrypi python3[2580]:   File "/home/pi/system_sensors/src/system_sensors.py", line 13, in <module>
Mar 14 23:23:27 raspberrypi python3[2580]:     from rpi_bad_power import new_under_voltage
Mar 14 23:23:27 raspberrypi python3[2580]: ModuleNotFoundError: No module named 'rpi_bad_power'
Mar 14 23:23:27 raspberrypi systemd[1]: system_sensors.service: Main process exited, code=exited, status=1/FAILURE
Mar 14 23:23:27 raspberrypi systemd[1]: system_sensors.service: Failed with result 'exit-code'.
pi@raspberrypi:~/system_sensors $ cd src
pi@raspberrypi:~/system_sensors/src $ ls
settings.yaml  system_sensors.py
pi@raspberrypi:~/system_sensors/src $ python3 system_sensors.py
usage: system_sensors.py [-h] settings
system_sensors.py: error: the following arguments are required: settings
pi@raspberrypi:~/system_sensors/src $ cd ..
pi@raspberrypi:~/system_sensors $ python3 src/system_sensors.py src/settings.yaml
send config message
import of apt failed!
Connected to broker

Raspberry Pi 4 (4GB) Latest Raspbian

michl270 commented 3 years ago

Same error is occuring on my fresh installation on a RPI3

Sennevds commented 3 years ago

Did you execute following command: sudo apt-get install python3-apt?

michl270 commented 3 years ago

yes i did - version 1.4.3 is already installed on my system

Sennevds commented 3 years ago

Could you try to disable the updates in the settings.yaml just to check if it then works

michl270 commented 3 years ago

I've just tried it without updates option.

Here is my settings.yaml: mqtt: hostname: 10.1.100.93 port: 1883 #defaults to 1883 user: homeassistant password: removed for data protection deviceName: deconz client_id: deconz timezone: Europe/Brussels update_interval: 60 #Defaults to 60 check_wifi_strength: true check_available_updates: false external_drives: Drive1: /boot/

Having rebooted after changing settings.yaml, the error still remains the same:

`pi@phoscon:~ $ sudo systemctl status system_sensors.service ● system_sensors.service - System Sensor service Loaded: loaded (/etc/systemd/system/system_sensors.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2021-03-23 21:11:35 UTC; 18s ago Process: 761 ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sensors.py /home/$ (code=exited, status=1/FAILURE) Main PID: 761 (code=exited, status=1/FAILURE)

Mär 23 21:11:33 phoscon systemd[1]: Started System Sensor service. Mär 23 21:11:35 phoscon python3[761]: File "/home/pi/system_sensors/src/system_sensors.py", line 96 Mär 23 21:11:35 phoscon python3[761]: print (f"Message received: {message.payload.decode()}" ) Mär 23 21:11:35 phoscon python3[761]: ^ Mär 23 21:11:35 phoscon python3[761]: SyntaxError: invalid syntax Mär 23 21:11:35 phoscon systemd[1]: system_sensors.service: Main process exited, code=exited, status=1/FAILURE Mär 23 21:11:35 phoscon systemd[1]: system_sensors.service: Unit entered failed state. Mär 23 21:11:35 phoscon systemd[1]: system_sensors.service: Failed with result 'exit-code'.`

When running pi@phoscon:~/system_sensors $ python3 src/system_sensors.py src/settings.yaml everything is working properly

Sennevds commented 3 years ago

Hmm that's weird. Could you send the contents of the system_sensor.service file?

michl270 commented 3 years ago

Sure :-)

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

[Service] User=pi Type=idle ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sensors.py /home/pi/system_sensors/src/settings.yaml

[Install] WantedBy=multi-user.target

i have just double checked, if it's working now as I have detected an error in my service-file. I have corrected the error copied the service file to the correct directory, and re-enabled the service. error still remains the same:

pi@phoscon:~/system_sensors $ sudo systemctl status system_sensors.service ● system_sensors.service - System Sensor service Loaded: loaded (/etc/systemd/system/system_sensors.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Tue 2021-03-23 21:28:53 UTC; 4s ago Process: 2489 ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sensors.py /home/pi/system_sensors/src/settings.yaml (code=exited, status=1/FAILURE) Main PID: 2489 (code=exited, status=1/FAILURE)

Mär 23 21:28:53 phoscon systemd[1]: Started System Sensor service. Mär 23 21:28:53 phoscon python3[2489]: File "/home/pi/system_sensors/src/system_sensors.py", line 96 Mär 23 21:28:53 phoscon python3[2489]: print (f"Message received: {message.payload.decode()}" ) Mär 23 21:28:53 phoscon python3[2489]: ^ Mär 23 21:28:53 phoscon python3[2489]: SyntaxError: invalid syntax Mär 23 21:28:53 phoscon systemd[1]: system_sensors.service: Main process exited, code=exited, status=1/FAILURE Mär 23 21:28:53 phoscon systemd[1]: system_sensors.service: Unit entered failed state. Mär 23 21:28:53 phoscon systemd[1]: system_sensors.service: Failed with result 'exit-code'.

Sennevds commented 3 years ago

What version of python are you running?

adamstratton commented 3 years ago

Had the same issue on a RPi4 and took a while to troubleshoot things. Sorted with the below. Used systemd-analyze plot > output.svg on before/after, looks like waiting for docker is part of the problem, starting it earlier after network-online.target works. Only have a couple of containers, one of them uses mqtt for HA related things. Still works after the change.

An FYI to others who may be having the same issue.

[Unit] Description=System Sensor service

After=multi-user.target

After=network-online.target Wants=network-online.target

[Service] User=pi Type=idle ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sensors.py /home/pi/system_sensors/src/settings.yaml

[Install] WantedBy=multi-user.target

system_sensors_before system_sensors_after
michl270 commented 3 years ago

What version of python are you running?

I am running 3.8.4

michl270 commented 3 years ago

Had the same issue on a RPi4 and took a while to troubleshoot things. Sorted with the below. Used systemd-analyze plot > output.svg on before/after, looks like waiting for docker is part of the problem, starting it earlier after network-online.target works. Only have a couple of containers, one of them uses mqtt for HA related things. Still works after the change.

An FYI to others who may be having the same issue.

[Unit] Description=System Sensor service

After=multi-user.target

After=network-online.target Wants=network-online.target

[Service] User=pi Type=idle ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sensors.py /home/pi/system_sensors/src/settings.yaml

[Install] WantedBy=multi-user.target

system_sensors_before system_sensors_after

Thanks for sharing your solution. Unfortunately, it seems to be not working for my setup on my RPI3. Still facing the same issuce.

sudo systemctl status system_sensors.service ● system_sensors.service - System Sensor service Loaded: loaded (/etc/systemd/system/system_sensors.service; enabled; vendor p Active: failed (Result: exit-code) since Mon 2021-03-29 05:52:53 UTC; 44s ago Process: 668 ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sen Main PID: 668 (code=exited, status=1/FAILURE)

Mär 29 05:52:51 phoscon systemd[1]: Started System Sensor service. Mär 29 05:52:53 phoscon python3[668]: File "/home/pi/system_sensors/src/system Mär 29 05:52:53 phoscon python3[668]: print (f"Message received: {message.pa Mär 29 05:52:53 phoscon python3[668]: Mär 29 05:52:53 phoscon python3[668]: SyntaxError: invalid syntax Mär 29 05:52:53 phoscon systemd[1]: system_sensors.service: Main process exited, Mär 29 05:52:53 phoscon systemd[1]: system_sensors.service: Unit entered failed Mär 29 05:52:53 phoscon systemd[1]: system_sensors.service: Failed with result ' lines 1-14/14 (END)...skipping... ● system_sensors.service - System Sensor service Loaded: loaded (/etc/systemd/system/system_sensors.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Mon 2021-03-29 05:52:53 UTC; 44s ago Process: 668 ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sensors.py /home/pi/system_sensors/src/settings.yaml (code=exited, status=1/FAILURE) Main PID: 668 (code=exited, status=1/FAILURE)

Mär 29 05:52:51 phoscon systemd[1]: Started System Sensor service. Mär 29 05:52:53 phoscon python3[668]: File "/home/pi/system_sensors/src/system_sensors.py", line 96 Mär 29 05:52:53 phoscon python3[668]: print (f"Message received: {message.payload.decode()}" ) Mär 29 05:52:53 phoscon python3[668]: ^ Mär 29 05:52:53 phoscon python3[668]: SyntaxError: invalid syntax Mär 29 05:52:53 phoscon systemd[1]: system_sensors.service: Main process exited, code=exited, status=1/FAILURE Mär 29 05:52:53 phoscon systemd[1]: system_sensors.service: Unit entered failed state. Mär 29 05:52:53 phoscon systemd[1]: system_sensors.service: Failed with result 'exit-code'.

Ukchris79 commented 3 years ago

I have the same issue on a 3B, manually I can start it, but it cannot auto start on reboot

Vayain commented 3 years ago

Sadly, same error for my, raspberry pi 3b - manual start works fine, start on reboot doesn't work - error "Network is unreachable"?

`pi@raspberrypi:~ $ sudo systemctl status system_sensors.service ● system_sensors.service - System Sensor service Loaded: loaded (/etc/systemd/system/system_sensors.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Fri 2021-05-07 15:31:18 CEST; 2min 49s ago Process: 454 ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sensors.py /home/pi/system_sensors/src/settings.yaml (code=exited, status=1 Main PID: 454 (code=exited, status=1/FAILURE)

Mai 07 15:31:18 raspberrypi python3[454]: sock = self._create_socket_connection() Mai 07 15:31:18 raspberrypi python3[454]: File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3522, in _create_socket_connect Mai 07 15:31:18 raspberrypi python3[454]: return socket.create_connection(addr, source_address=source, timeout=self._keepalive) Mai 07 15:31:18 raspberrypi python3[454]: File "/usr/lib/python3.7/socket.py", line 727, in create_connection Mai 07 15:31:18 raspberrypi python3[454]: raise err Mai 07 15:31:18 raspberrypi python3[454]: File "/usr/lib/python3.7/socket.py", line 716, in create_connection Mai 07 15:31:18 raspberrypi python3[454]: sock.connect(sa) Mai 07 15:31:18 raspberrypi python3[454]: OSError: [Errno 101] Network is unreachable Mai 07 15:31:18 raspberrypi systemd[1]: system_sensors.service: Main process exited, code=exited, status=1/FAILURE Mai 07 15:31:18 raspberrypi systemd[1]: system_sensors.service: Failed with result 'exit-code'. lines 1-16/16 (END)`

Sennevds commented 3 years ago

Sadly, same error for my, raspberry pi 3b - manual start works fine, start on reboot doesn't work - error "Network is unreachable"?

`pi@raspberrypi:~ $ sudo systemctl status system_sensors.service ● system_sensors.service - System Sensor service Loaded: loaded (/etc/systemd/system/system_sensors.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Fri 2021-05-07 15:31:18 CEST; 2min 49s ago Process: 454 ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sensors.py /home/pi/system_sensors/src/settings.yaml (code=exited, status=1 Main PID: 454 (code=exited, status=1/FAILURE)

Mai 07 15:31:18 raspberrypi python3[454]: sock = self._create_socket_connection() Mai 07 15:31:18 raspberrypi python3[454]: File "/home/pi/.local/lib/python3.7/site-packages/paho/mqtt/client.py", line 3522, in _create_socket_connect Mai 07 15:31:18 raspberrypi python3[454]: return socket.create_connection(addr, source_address=source, timeout=self._keepalive) Mai 07 15:31:18 raspberrypi python3[454]: File "/usr/lib/python3.7/socket.py", line 727, in create_connection Mai 07 15:31:18 raspberrypi python3[454]: raise err Mai 07 15:31:18 raspberrypi python3[454]: File "/usr/lib/python3.7/socket.py", line 716, in create_connection Mai 07 15:31:18 raspberrypi python3[454]: sock.connect(sa) Mai 07 15:31:18 raspberrypi python3[454]: OSError: [Errno 101] Network is unreachable Mai 07 15:31:18 raspberrypi systemd[1]: system_sensors.service: Main process exited, code=exited, status=1/FAILURE Mai 07 15:31:18 raspberrypi systemd[1]: system_sensors.service: Failed with result 'exit-code'. lines 1-16/16 (END)`

do you have following lines in your systemd file: After=network-online.target Wants=network-online.target I don't have any problems and use it on 5 different PI's some on wifi some on ethernet so for some reason your PI takes a long time to connect

Sennevds commented 3 years ago

Had the same issue on a RPi4 and took a while to troubleshoot things. Sorted with the below. Used systemd-analyze plot > output.svg on before/after, looks like waiting for docker is part of the problem, starting it earlier after network-online.target works. Only have a couple of containers, one of them uses mqtt for HA related things. Still works after the change. An FYI to others who may be having the same issue. [Unit] Description=System Sensor service

After=multi-user.target

After=network-online.target Wants=network-online.target [Service] User=pi Type=idle ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sensors.py /home/pi/system_sensors/src/settings.yaml [Install] WantedBy=multi-user.target

system_sensors_before system_sensors_after

Thanks for sharing your solution. Unfortunately, it seems to be not working for my setup on my RPI3. Still facing the same issuce.

sudo systemctl status system_sensors.service ● system_sensors.service - System Sensor service Loaded: loaded (/etc/systemd/system/system_sensors.service; enabled; vendor p Active: failed (Result: exit-code) since Mon 2021-03-29 05:52:53 UTC; 44s ago Process: 668 ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sen Main PID: 668 (code=exited, status=1/FAILURE)

Mär 29 05:52:51 phoscon systemd[1]: Started System Sensor service. Mär 29 05:52:53 phoscon python3[668]: File "/home/pi/system_sensors/src/system Mär 29 05:52:53 phoscon python3[668]: print (f"Message received: {message.pa Mär 29 05:52:53 phoscon python3[668]: Mär 29 05:52:53 phoscon python3[668]: SyntaxError: invalid syntax Mär 29 05:52:53 phoscon systemd[1]: system_sensors.service: Main process exited, Mär 29 05:52:53 phoscon systemd[1]: system_sensors.service: Unit entered failed Mär 29 05:52:53 phoscon systemd[1]: system_sensors.service: Failed with result ' lines 1-14/14 (END)...skipping... ● system_sensors.service - System Sensor service Loaded: loaded (/etc/systemd/system/system_sensors.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Mon 2021-03-29 05:52:53 UTC; 44s ago Process: 668 ExecStart=/usr/bin/python3 /home/pi/system_sensors/src/system_sensors.py /home/pi/system_sensors/src/settings.yaml (code=exited, status=1/FAILURE) Main PID: 668 (code=exited, status=1/FAILURE)

Mär 29 05:52:51 phoscon systemd[1]: Started System Sensor service. Mär 29 05:52:53 phoscon python3[668]: File "/home/pi/system_sensors/src/system_sensors.py", line 96 Mär 29 05:52:53 phoscon python3[668]: print (f"Message received: {message.payload.decode()}" ) Mär 29 05:52:53 phoscon python3[668]: ^ Mär 29 05:52:53 phoscon python3[668]: SyntaxError: invalid syntax Mär 29 05:52:53 phoscon systemd[1]: system_sensors.service: Main process exited, code=exited, status=1/FAILURE Mär 29 05:52:53 phoscon systemd[1]: system_sensors.service: Unit entered failed state. Mär 29 05:52:53 phoscon systemd[1]: system_sensors.service: Failed with result 'exit-code'.

Sorry for the late reply which version of python are you using? This is a different error then the network error: SyntaxError: invalid syntax But this is good syntax so my guess is that u use an old version of python

cdn4lf commented 3 years ago

I just added in ExecStartPre=/bin/sleep 60 and everything appears to be working now. It looks like it was trying to connect before my mqtt server was up

cdn4lf commented 3 years ago

So I added a restart to the service file and set it to restart 5 times with a 60 second delay. This seems to have resolved my issue.

What seemed to be happening is the service would start on network but that was before mqtt since mqtt booted with docker/home assistant. Adding the condition on failure to my restart condition seems to have sorted this out. Currently I haven't seen it need more than one restart.