0

I am running a Paho MQTT client on my raspberry pi (Stretch OS). This script recieves MQTT messages from a publisher script and then stores the data in a SQL database. As I added client.loop_forever() at the end of the script, it can continuously listen for published messages. I can confirm that the script works when I run it manually on terminal as the SQL database becomes updated with new data (as well as printing output in terminal). When I try to configure it for auto start however, it does not seem to work.

I have tried several ways to autostart the script on boot up. I have first used crontab. In crontab, the line I entered reads like this: @reboot python /home/user/project/MQTTclientscript.py I tried restarting the device a few times, but it does not work (i.e. does not send the data into my SQL database which otherwise works when I run manually on terminal). The other programs that are also run via crontab works e.g. 10 * * * * python /home/user/project/demoproject.py

I then tried to rephrase the line in crontab like this: @reboot /usr/bin/python /home/user/project/MQTTclientscript.py, but it still does not work after I reboot. The other programs in crontab still works via this method.

I then tried to edit another file as an alternative approach by typing sudo nano /etc/rc.local in terminal. I then added python /home/user/project/demoproject.py above the line exit 0. This approach however did not work when I rebooted the device.

Running out of options, I then tried to access crontab via sudo crontab -e. I listed my startup programs into the file and rebooted the device. Again, the MQTT client program did not work (i.e. update SQL database).

To troubleshoot, I entered @reboot python /home/user/project/MQTTclientscript.py> output_file.txt in crontab. There is a blank output file produced which I think shows that the script is running.

My question is if it is running, why is it not working in the background as I want it to be, upon boot up? When I run the script from bash commands in terminal, it works well however. Any thought on this?

  • why is it not working in the background -> It's not clear from the question that you did anything to make it run in the background. Unless it forks itself, it's implicit that it will thus run in the foreground, and it will quickly get killed or die that way. – goldilocks Dec 23 '17 at 20:38
  • I've added ` client.loop_forever()` at the end of the script so it continuously loops to listen for incoming messages. – Craver2000 Dec 23 '17 at 20:52
1

While you can use cron to start something when the system boots, it's not a great solution: it's not terribly flexible, it won't handle restarting the service for you automatically, and it doesn't provide you with any easy way to stop or start the service.

Your device has a facility to automatically start services when it boots called "systemd". To have systemd launch your script when the system boots:

  1. Create a file /etc/systemd/system/mqttclient.service with the contents:

    [Service]
    Type=simple
    ExecStart=/usr/bin/python /home/user/project/MQTTclientscript.py
    
    [Install]
    WantedBy=multi-user.target
    
  2. Run systemctl enable mqttclient. This will cause your script to start next time the system boots.

  3. To start the service immediately, run systemctl start mqttclient.

Any output generated by the script will be collected by the system journal; you can view this by running journalctl -u myqttclient.service.


The above will run your python script as root. If you want it to run under you userid instead, modify the mqttclient.service file to look like:

[Service]
Type=simple
User=yourusername
ExecStart=/usr/bin/python /home/user/project/MQTTclientscript.py

[Install]
WantedBy=multi-user.target

You can also configure the service to restart automatically if it fails; for this and other details see the systemd.service man page.

  • Thanks larsks for your advice. For running as either root, or under my userid, running systemctl enable mqttclient or systemctl start mqttclient leads to a prompt for a password which I input the same when I log in with ssh, however, it then states polkit-agent-helper-1: pam_authenticate failed: Authentication failure ==== AUTHENTICATION FAILED === Any ideas why? – Craver2000 Dec 23 '17 at 22:54
  • Sorry, I should have mentioned, you generally can only run systemctl as root (so you would run sudo systemctl ...). – larsks Dec 24 '17 at 4:23
  • Thanks larsks, I no longer have the prompts after adding sudo to those two commands. Unfortunately, this approach doesnt seem to help to autostart this MQTT client properly. However, I did something similar to this post that I just found (raspberrypi.org/forums/viewtopic.php?t=192845) and added some time delay (10s). This appeared to resolve the issue. Apparently, the problem is thought to arise as the network has not yet started on boot up. – Craver2000 Dec 24 '17 at 11:07

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.