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.