7

I am writing a python script that subscribes to some MQTT topics to localhost MQTT broker and when a message is pushed, the script will call a function in another script on the same directory to load the changes into an SQL database.

The script is working fine when run manually in terminal:

python3 /directory/path/to/file/listen_mqtt.py

However, I am trying to make this file execute automatically on Ubuntu system startup. I have created in a new service in:

/lib/systemd/system/listen_mqtt_py.service

The service description goes as follow:

[Unit]
Description=Listen Mqtt
After=mosquitto.service
Wants=network.target
[email protected]

[Service]
Type=simple
ExecStart=/usr/bin/python3 /home/bt/dev/dexter-mqtt-to-sql/listen_mqtt.py
StandardInput=tty-force

[Install]
WantedBy=multi-user.target

I also enabled the service and tried starting the service using:

sudo systemctl enable listen_mqtt_py.service

and

sudo systemctl start listen_mqtt_py.service

When rebooting the machine and also try running the service manually, I get the folling messages:

sudo systemctl status listen_mqtt_py.service
● listen_mqtt_py.service - Listen Mqtt
     Loaded: loaded (/lib/systemd/system/listen_mqtt_py.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Tue 2020-11-17 13:45:28 AEDT; 14s ago
    Process: 2206 ExecStart=/usr/bin/python3 /home/bt/dev/dexter-mqtt-to-sql/listen_mqtt.py (code=exited, status=1/FAILURE)
   Main PID: 2206 (code=exited, status=1/FAILURE)

Nov 17 13:45:27 btdms systemd[1]: Started Listen Mqtt.
Nov 17 13:45:28 btdms systemd[1]: listen_mqtt_py.service: Main process exited, code=exited, status=1/FAILURE
Nov 17 13:45:28 btdms systemd[1]: listen_mqtt_py.service: Failed with result 'exit-code'.

I did some research and found that this type of error could be related to calling the service too early before some required device has been loaded. Thus I tried altering the After= to network-online.target and also to mosquitto.service, however with no luck, service still exited with the same error messages.

Because the service does not fully execute even when I run sudo systemctl start listen_mqtt_py.service manually, so I suspect this is not due to another service not being loaded in time. It is some other reason. But I could not figure out why.

I am happy to also post the python script listen_mqtt.py if needed.

Thanks.

2 Answers 2

11

Turns out this is due to user privilege for python packages. Method to diagnosing this issue can be found here.

Lets find the systemd services which fail to start

$ systemctl --failed

Found a problem with systemd-modules-load service. We want to know more.

$ systemctl status systemd-modules-load  

Restart service if needed to regenerate the failling messge

$ systemctl restart systemd-modules-load

Now with the PID obtained:

journalctl _PID=15630

or

journalctl _SYSTEMD_UNIT=systemd-modules-load.service

You should be able to see the error message generated on failure. Looking at the error messages, we know that the paho package has not been imported properly.

Solution

a. Install the related python package using root (which is what I did eventually)

To enter root CLI

sudo su -

b. Give root privilege to the python packages required in the service, I did not do this because I am not sure how it will behave with other users and I am not sure if root needs more privilege than a package only. Discussion about this can be found here.

1
  • journalctl _SYSTEMD_UNIT=systemd-modules-load.service this command helps me to find the full log. Nov 6, 2023 at 10:05
2

journalctl -u charge_check.service -b to chech what is going on wrong

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .