jivaro
jivaro

Reputation: 41

Cannot execute a mqtt server after booting a Raspberry with crontab -e / @reboot

I have programmed in Python several mqtt driven servers on a Raspberry Pi W (with Buster) to execute a set of commands when receiving a given mqtt message from the mqtt broker after subscribing to a set of topics (turn on/off gpios, read and publish through mqtt temperatures, etc...).

This Raspberry is to be installed on a remote location and I want to execute these programs after booting so as to guarantee that they will be active even if a temporary loss of electrical supply happens and the Raspi reboots after the blackout.

I have included a @reboot entry in "sudo crontab -e" to launch a bash file starting the several servers after booting:

@reboot /usr/bin/sleep 60 && /home/pi/p/mqtt/mqtt201.sh

The sleep 60 is to give time the Raspberry to finish the booting process before starting the programs.

The bash file to execute is quite simple:

#!/bin/bash
/usr/bin/python3 /home/pi/p/mqtt/alive201.py >> /home/pi/p/mqtt/alive201.log 2>&1 &
/usr/bin/python3 /home/pi/p/mqtt/led201-05.py >> /home/pi/p/mqtt/led201-05.log 2>&1 &
/usr/bin/python3 /home/pi/p/mqtt/led201-27.py >> /home/pi/p/mqtt/led201-27.log 2>&1 &
/usr/bin/python3 /home/pi/p/mqtt/temp201b_mqtt.py >> /home/pi/p/mqtt/temp201b_mqtt.log 2>&1 &

After booting, when I ssh to the Raspi none of the servers are running because all of them have exited after an error signaled as follows:

Traceback (most recent call last):
  File "/home/pi/p/mqtt/alive201.py", line 1, in <module>
    import paho.mqtt.client as mqtt
ModuleNotFoundError: No module named 'paho'
Traceback (most recent call last):
  File "/home/pi/p/mqtt/alive201.py", line 1, in <module>
    import paho.mqtt.client as mqtt
ModuleNotFoundError: No module named 'paho'

In other words, for some reason, when the programs are executed, paho-mqtt module is not loaded and is not available for the programs to execute.

If after booting I ssh to the Raspberry and I run the bash file manually, the servers are started and everything goes fine.

Does anybody know how can I start my servers through crontab @reboot option with the bash script without facing the problem of the non-availability of paho-mqtt module ?

Thanks and best regards

Upvotes: 0

Views: 490

Answers (1)

jivaro
jivaro

Reputation: 41

Having spent most of my weekend trying to figure out unsuccessfully how to launch my bash script after boot by means of crontab -e / @reboot, I have shifted to systemd services to launch the script after boot.

Just for the sake of curiosity of those that may be interested on this option, the server configuration file that I have prepared is the following one:

pi@PiZeroW-1:~ $ cat /etc/systemd/system/201server.serviceenter
[Unit]
Description=201 server service
After=multi-user.target
Requires=network.target
[Service]
Type=idle
User=pi
ExecStart=/home/pi/p/mqtt/mqtt201.sh
Restart=no
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target`

After booting, when checking the service status this is what I got:

pi@PiZeroW-1:~ $ sudo systemctl status 201server.service
● 201server.service - 201 server service
   Loaded: loaded (/etc/systemd/system/201server.service; enabled; vendor preset: enabled)
   Active: active (exited) since Sun 2021-11-28 19:22:27 GMT; 8min ago
   Process: 464 ExecStart=/home/pi/p/mqtt/mqtt201.sh (code=exited, status=0/SUCCESS)
   Main PID: 464 (code=exited, status=0/SUCCESS)
Tasks: 4 (limit: 725)
   CGroup: /system.slice/201server.service
           ├─494 /usr/bin/python3 /home/pi/p/mqtt/alive201.py
           ├─495 /usr/bin/python3 /home/pi/p/mqtt/led201-05.py
           ├─496 /usr/bin/python3 /home/pi/p/mqtt/led201-27.py
           └─497 /usr/bin/python3 /home/pi/p/mqtt/temp201b_mqtt.py

nov 28 19:22:27 PiZeroW-1 systemd[1]: Started 201 server service.

and when checking if all the processes were active, everything was OK:

pi@PiZeroW-1:~ $ ps -ax | grep 201
  494 ?        S      0:02 /usr/bin/python3 /home/pi/p/mqtt/alive201.py
  495 ?        S      0:02 /usr/bin/python3 /home/pi/p/mqtt/led201-05.py
  496 ?        S      0:02 /usr/bin/python3 /home/pi/p/mqtt/led201-27.py
  497 ?        S      0:07 /usr/bin/python3 /home/pi/p/mqtt/temp201b_mqtt.py
 1190 pts/0    S+     0:00 grep --color=auto 201

Summarizing: forget crontab-e to start processes after boot and let's go to systemd.

I hope this helps to save time to other people in a similar situation as mine.

Upvotes: 0

Related Questions