0

I'm new to the Raspberry Pi, but have gotten mosquitto to work & a similar mosquitto service running on one pi, but when I try running it on another Pi I encounter an error. For context, the service runs a python3 script that acts as a subscriber for mosquitto running on the Pi. Messages are sent from an iOS app. All works fine if I run as: python3 control-baldwinbot.py # which is my subscriber python code and, as Ingo suggest below, this also works fine /usr/bin/python3 /home/pi/control-baldwinbot.py but I can't seem to get a script (working on another Pi) to work as written, below:

The .service file is saved as: /lib/systemd/system/baldwinbot-mqttclient.service

and it contains this script:

[Unit]
Description=Baldwinbot MQTT Script Service
After=mosquitto.service

[Service]
Type=idle
ExecStart=/usr/bin/python3 /home/pi/control-baldwinbot.py
Restart=always
[Install]
WantedBy=multi-user.target

When I run a sudo systemctl status baldwinbot-mqttclient.service I get this output:

● baldwinbot-mqttclient.service - Baldwinbot MQTT Script Service
   Loaded: loaded (/lib/systemd/system/baldwinbot-mqttclient.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2019-02-26 21:49:12 GMT; 32s ago
  Process: 763 ExecStart=/usr/bin/python3 /home/pi/control-baldwinbot.py (code=exited, status=1/FAILURE)
 Main PID: 763 (code=exited, status=1/FAILURE)

Feb 26 21:49:12 baldwinbot systemd[1]: baldwinbot-mqttclient.service: Failed with result 'exit-code'.
Feb 26 21:49:12 baldwinbot systemd[1]: baldwinbot-mqttclient.service: Service hold-off time over, scheduling restart.
Feb 26 21:49:12 baldwinbot systemd[1]: Stopped Baldwinbot MQTT Script Service.
Feb 26 21:49:12 baldwinbot systemd[1]: baldwinbot-mqttclient.service: Start request repeated too quickly.
Feb 26 21:49:12 baldwinbot systemd[1]: Failed to start Baldwinbot MQTT Script Service.
Feb 26 21:49:12 baldwinbot systemd[1]: baldwinbot-mqttclient.service: Unit entered failed state.
Feb 26 21:49:12 baldwinbot systemd[1]: baldwinbot-mqttclient.service: Failed with result 'exit-code'.

mosquitto is running, checked via: sudo systemctl is-active mosquitto.service with the returned response: active

Thanks for your help!

  • it looks like /home/pi/control-baldwinbot.py exits with a status 1/FAILURE - perhaps it needs to be run as pi not root – Jaromanda X Feb 26 at 22:01
  • Thanks Jaromanda X, I've added User=pi before Restart=always and but I still fail when I try to enable the service. Curiously, I can start the service. Below is the output from the two commands & a check if it is_active. When I reboot, the service does not run on its own. pi@baldwinbot:~ $ sudo systemctl enable baldwinbot-mqttclient.service pi@baldwinbot:~ $ sudo systemctl is-active baldwinbot-mqttclient.service failed pi@baldwinbot:~ $ sudo systemctl start baldwinbot-mqttclient.service pi@baldwinbot:~ $ sudo systemctl is-active baldwinbot-mqttclient.service active – Gallaugher Feb 27 at 23:25
1

Using Type=idle without console output and Restart=always in the unit file does not look very professional. If control-baldwinbot.py doesn't give you any error messages why it fails then you are lost. You may have a look at journalctl -b -e if you find some hints from control-baldwinbot.py. If not then you should consider to use a more professional service that does not silently fails and does not try to restart endless anyway.

To be a bit more specific about my rant ;-) In man systemd.service you can find about Type=idle

Behavior of idle is very similar to simple; however, actual execution of the service program is delayed until all active jobs are dispatched. This may be used to avoid interleaving of output of shell services with the status output on the console. Note that this type is useful only to improve console output, it is not useful as a general unit ordering tool, and the effect of this service type is subject to a 5s time-out, after which the service program is invoked anyway.

I cannot see any useful needs for the option in this context. As far as I see there is no console output that must be ordered and a delay of 5 seconds may be rather counterproductive.

Trying endless to restart a failing service without error handling is simply bad program engineering. This may result in a deadlock or an overload of the whole device. And this overload takes already place in your case. Fortunately systemd has an emergency brake for this programs having gone wild. systemd stopped it with the message baldwinbot-mqttclient.service: Start request repeated too quickly.

I suggest you try to make your own service for control-baldwinbot.py. Because the script is running successfully from the command line we only have to ensure the same environment within the service. Create a new service with:

rpi ~$ sudo systemctl --force --full edit my-baldwinbot-mqttclient.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=My Baldwinbot MQTT Script Service
After=mosquitto.service

[Service]
User=pi
WorkingDirectory=/home/pi
ExecStart=/usr/bin/python3 /home/pi/control-baldwinbot.py

[Install]
WantedBy=multi-user.target

Disable the old service and enable this one:

rpi ~$ sudo systemctl mask baldwinbot-mqttclient.service
rpi ~$ sudo systemctl enable my-baldwinbot-mqttclient.service

Reboot.

If it doesn't work then we only have to look what's different from the commandline execution and fix it.

  • Thanks for the hit. Entering the command above runs the code, no problem. I've edited the original question to provide a bit more context, but the specifics re: mosquitto & iOS app are likely not important. Also - unsure what you advise when yo usuggest the file "does not look very professional". Is there something additional you might advise here? Thanks again! – Gallaugher Feb 27 at 23:15
  • @Gallaugher Please apologize my rant if you have made the Unit. It is not clear from the question. I have thought it is from the vendor. I have updated my answer. – Ingo Feb 28 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.