Shadow_Saunter
Posts: 80
Joined: Thu Mar 22, 2018 2:04 pm

Cron is not starting my python MQTT script correctly

Sun Mar 10, 2019 3:03 am

I'm trying to start a script at reboot that reads my BME680 and sends the data to an MQTT broker.

The script runs perfectly as my unprivileged user, but when i try to launch it from a cron job it never connects to the broker.

I can see that it is running by grep'ing for python, but:
1. nothing appears when i try to redirect STDOUT into a log file.
2. the broker never responds with a client connection message

My cron entry looks like this:

Code: Select all

@reboot python3 ~/sensor.py & >> log.txt
and my python script uses the following imports:

Code: Select all

import paho.mqtt.client as mqtt
import threading
import bme680
import time
import json
My mqtt connects on a separate thread, sort of like this:

Code: Select all

def connect(mqttc):
    mqttc.connect(MQTT_Broker, MQTT_Port, Interval)
    mqttc.loop_forever()
    
def main_loop():
    mqttc = mqtt.Client()
    T1 = threading.Thread(target=connect, args=(mqttc,))
    T1.daemon = True
    T1.start()
    while True: 
        poll_sensors()
        time.sleep(1200)
main_loop()
    

Help?

User avatar
B.Goode
Posts: 12051
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Cron is not starting my python MQTT script correctly

Sun Mar 10, 2019 8:00 am

Code: Select all

@reboot python3 ~/sensor.py & >> log.txt
Replace ~/sensor.py with an explicit path to your script. Something like /home/pi/sensor.py, but modified as appropriate.

You might also need an explicit path to the Python3 interpreter, but one thing at a time so you learn what the problem was.

(The notation ~/sensor.py says look in the Home directory for the current user. The system Cron process, and the startup process, run as the root user so your configuration is almost certainly looking in the wrong place for the script.)

User avatar
thagrol
Posts: 4916
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Cron is not starting my python MQTT script correctly

Sun Mar 10, 2019 12:06 pm

Shadow_Saunter wrote:
Sun Mar 10, 2019 3:03 am
My cron entry looks like this:

Code: Select all

@reboot python3 ~/sensor.py & >> log.txt
That won't capture any error output. Change it to

Code: Select all

@reboot python3 ~/sensor.py & >> log.txt 2>&1
B.Goode's suggestions only apply if you're using root's crontab not that of your normal user. Which I guess you aren't as you say the script is showing in the process list.

Doesn't mqtt require networking toi be up? Cron may be running your code before it is. I guess it's also possible that cron starts your script before the mqtt broker is ready.

You might get better results by switching to running your script as a systemd service where you can make it dependent on both netwroking and the mqtt broker being up. Hit google for systemd docs.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

Shadow_Saunter
Posts: 80
Joined: Thu Mar 22, 2018 2:04 pm

Re: Cron is not starting my python MQTT script correctly

Mon Mar 11, 2019 5:48 pm

thagrol wrote:
Sun Mar 10, 2019 12:06 pm
You might get better results by switching to running your script as a systemd service where you can make it dependent on both netwroking and the mqtt broker being up. Hit google for systemd docs.
Thanks, adding a systemd service worked fine.

For future visitors, this link helped with the setup:
https://raspberrypi.stackexchange.com/q ... 5220#95220

and this thread within that link advised that building in a sleep for 10 seconds or so before contacting the broker is also successful:
viewtopic.php?t=192845

Edit:
and of course, systemd docs:
https://www.freedesktop.org/wiki/Software/systemd/

Shadow_Saunter
Posts: 80
Joined: Thu Mar 22, 2018 2:04 pm

Re: Cron is not starting my python MQTT script correctly

Wed Mar 13, 2019 1:32 am

Update. I gave up on using systemd to start my script because I can't get it to use a launch a thread within my python script, as pictured above. I instead built a 10 second delay into my python script before it tries to interface with MQTT, and it is running fine. The 10 second delay is to allow networking services to start before publishing to mosquitto, of course.

I suspect that I have a fundamental lack of understanding of what running this as a service with systemd is, and how that differs from what I am doing with my script. If anyone reading this can explain, I would love some help.

After I figure it out, I might still change it to connect to mosquitto as a service in another small script, and periodically publish via a crontab interval, but for right now, the above works fine.

Andyroo

Re: Cron is not starting my python MQTT script correctly

Wed Mar 13, 2019 1:47 am

If you are stuck as your program is waiting for networking you can set this at system level from raspi-config under ‘wait for network’.

PhatFil
Posts: 1705
Joined: Thu Apr 13, 2017 3:55 pm
Location: Oxford UK

Re: Cron is not starting my python MQTT script correctly

Wed Mar 13, 2019 6:15 am

try

Code: Select all

@reboot python3 /home/pi/sensor.py >> log.txt

User avatar
thagrol
Posts: 4916
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Cron is not starting my python MQTT script correctly

Wed Mar 13, 2019 12:23 pm

PhatFil wrote:
Wed Mar 13, 2019 6:15 am
try

Code: Select all

@reboot python3 /home/pi/sensor.py >> log.txt
Not relevant, the script is launching but not correctly/as expected. And that won't capture any error messages into the log
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

User avatar
DougieLawson
Posts: 41297
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Cron is not starting my python MQTT script correctly

Wed Mar 13, 2019 6:16 pm

If you do it in systemd you can make your service file dependent on the network

Code: Select all

[Unit]
Description=MQTT publisher
After=network-online.target mosquitto.service

[Service]
ExecStart=/usr/local/bin/mqttPub.py
Restart=always
User=pi
Group=pi
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=mqttPub

[Install]
WantedBy=multi-user.target
The After clause means that publisher service can't start before the network and before mosquitto.

In the script you need to be defensive in programming the connect().

Code: Select all

def connect(mqttc):
    tries = 5
    rc = mqttc.connect(MQTT_Broker, MQTT_Port, Interval, true)
    while rc !=0:
        try:
            rc = self.mqttc.reconnect()
            print "Reconnect rc:", rc
            if rc != 0:
                rc = mqttc.connect(MQTT_broker, MQTT_port, 60, True)
                    if rc != 0:
                        print "Connect rc: ", rc
                        raise Exception('connectionError')
        except:
            tries -= 1
            self.mqttc.disconnect()
            if tries == 0:
                raise Exception('connectionException')
            print "Tries:", tries, "rc:", rc
            time.sleep(0.5)
    
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Shadow_Saunter
Posts: 80
Joined: Thu Mar 22, 2018 2:04 pm

Re: Cron is not starting my python MQTT script correctly

Wed Mar 13, 2019 10:30 pm

DougieLawson wrote:
Wed Mar 13, 2019 6:16 pm
If you do it in systemd you can make your service file dependent on the network
Thanks, I'll try this.

User avatar
bonzadog
Posts: 272
Joined: Wed Apr 25, 2012 9:40 am
Location: Rietberg/Germany

Re: Cron is not starting my python MQTT script correctly

Tue Aug 25, 2020 2:30 pm

I have exactly the same problem.

Using a Raspberry Pi 3B using the OS BUSTER

entry in crontab -e

@reboot python3 /home/pi/mqtt-publish-heartbeat.py > /home/pi/mqtt-pub-hb1.log &

pub-hb1.log shows
Error on connecting to Broker : 192.168.178.109 at 2020-08-25 16:21:58

BUT executing on the command input
python3 /home/pi/mqtt-publish-heartbeat.py
gives
pi@mqtt:~ $ python3 /home/pi/mqtt-publish-heartbeat.py
Heartbeat1
Heartbeat2
Heartbeat3
Heartbeat4
Heartbeat5
Heartbeat6

So somehow the mqtt in reboot has a problem - but what??

Any help here please???

I prefer crontab - later I'll need to use another programme start there as well

User avatar
scruss
Posts: 4081
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Cron is not starting my python MQTT script correctly

Tue Aug 25, 2020 4:57 pm

bonzadog wrote:
Tue Aug 25, 2020 2:30 pm

Code: Select all

@reboot python3 /home/pi/mqtt-publish-heartbeat.py   > /home/pi/mqtt-pub-hb1.log &
Because the network's not up when @reboot runs. Either use Dougie's systemd solution - viewtopic.php?f=28&t=235460&p=1717760#p1441713 - or make your crontab line something like:

Code: Select all

@reboot sleep 30; python3 /home/pi/mqtt-publish-heartbeat.py   > /home/pi/mqtt-pub-hb1.log &
All that does is wait 30 seconds after reboot, by which time the network should be up and mqtt services are available. But if you have an unusual network, it'll likely fail. Use the systemd method.

You can't have "I need to start this immediately after boot" and "I need to use network services", but you can have "I need to start this immediately after the network starts".
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

User avatar
bonzadog
Posts: 272
Joined: Wed Apr 25, 2012 9:40 am
Location: Rietberg/Germany

Re: Cron is not starting my python MQTT script correctly

Wed Sep 23, 2020 11:45 am

I should have told you haw my script works.

On start the Programme loops ( a 5 sec delay in the loop) until a Network is available.
So when the System starts the programme will wait until a Network is available.

The Python programme doesn't even get started. So there must be something fundamentally wrong..but I still cannot find it.

As I wrote when I start using the CLI it works fine. With or without the Network being available it does exactly what it should.

Perhaps this may help..but so far I am stuck....

User avatar
neilgl
Posts: 3242
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Cron is not starting my python MQTT script correctly

Wed Sep 23, 2020 1:02 pm

Can you post your script so we can look at it / test it?

User avatar
bonzadog
Posts: 272
Joined: Wed Apr 25, 2012 9:40 am
Location: Rietberg/Germany

Re: Cron is not starting my python MQTT script correctly

Wed Sep 23, 2020 2:25 pm

OK here is the trial verion
-----------------------------------------------------------------------------------------------------------------

Code: Select all

# Improvement : coming up soon
# Status - cannot get this to start in CRON
# WORKS ON CLI   python3 /home/pi(Heartbeat.py
#


# Import package
import paho.mqtt.client as mqtt
import time 
import datetime
import sys

# Status - cannot get this to start in CRON
# WORKS ON CLI   python3 /home/pi(Heartbeat.py



# Define Variables
MQTT_BROKER = "xxx.xxx.xxx.xxx"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "mqtt-heartbeat"
Connect_Flag=False


# Define on_connect event Handler
# -------------------------------------------------------------------
def on_connect(mosq, obj, rc):
    print ("Connected to MQTT Broker")
# -------------------------------------------------------------------
# Define on_publish event Handler
def on_publish(client, userdata, mid):
    try:
        mqttc = mqtt.Client()
    except:
        print ("Error on Client - Broker probaly not available")
        log.write("Error on client (mqqt.client) ")
        sys.exit() 
# -------------------------------------------------------------------
# Register Event Handlers

#####################################################################
##        Programme Start
#####################################################################
mqttc = mqtt.Client()
mqttc.on_publish = on_publish
mqttc.on_connect = on_connect

try:
    log=open("d:/logfile.txt","w")
except:
    print ("Opening log file failed")
    exit()

# Connect with MQTT Broker as a loop so it gets retried every 10 seconds.
while True:

    try:
        mqttc.connect(MQTT_BROKER, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL) 
    #       Connect_Error = False
    except:
        t_connect= "Error on connecting to Broker : " + MQTT_BROKER + " at " + time.strftime('%Y-%m-%d %H:%M:%S')
        #    print (t_connect )
        if not Connect_Flag:
            log.write(t_connect+"\n")  # write error once only to log
        Error_FLAG=True
        time.sleep(10)                     # sleep for 20 seconds and then retry forever
        continue

# ..... end of first while true .......
try_counter = 5
while True:
# Test if a disconnection after a successful connect.

# Publish message to MQTT Topic 
    time_value=time.strftime('{%Y-%m-%d %H:%M:%S}')
    MQTT_MSG = "Heartbeat"+str(Counter)
    Counter= Counter + 1
    #  print (MQTT_MSG)
    try:
        mqttc.publish(MQTT_TOPIC,MQTT_MSG)
    except :
        Error_mess= "ERROR in publish at " + time_Value
        log.write(Error_mess,MQTT_MSG)
        print (Error_mess,MQTT_MSG)
        try_counter = try_counter - 1
        if try_counter == 0:
            break
    time.sleep(4)
# Disconnect from MQTT_Broker
mqttc.disconnect()
log.close()
exit()

User avatar
thagrol
Posts: 4916
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Cron is not starting my python MQTT script correctly

Wed Sep 23, 2020 5:31 pm

bonzadog wrote:
Wed Sep 23, 2020 2:25 pm
try:
log=open("d:/logfile.txt","w")
except:
print ("Opening log file failed")
exit()
Unless you're running on windows that will always throw an exception. "d:/logfile.txt" is not a valid linux path.

It would fail regardless of whether you're running the script from cron, systemd, or a shell prompt.

So either:
  1. That isn't the code you've successfully run from a shell prompt.
  2. Your code is failing before that point.
  3. There's something else you've not told us.
  4. All of the above
Oh, and shouldn't that be sys.exit() not exit()?
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

User avatar
bonzadog
Posts: 272
Joined: Wed Apr 25, 2012 9:40 am
Location: Rietberg/Germany

Re: Cron is not starting my python MQTT script correctly

Wed Sep 23, 2020 5:47 pm

thagrol wrote:
Wed Sep 23, 2020 5:31 pm
bonzadog wrote:
Wed Sep 23, 2020 2:25 pm
try:
log=open("d:/logfile.txt","w")
except:
print ("Opening log file failed")
exit()
Unless you're running on windows that will always throw an exception. "d:/logfile.txt" is not a valid linux path.

It would fail regardless of whether you're running the script from cron, systemd, or a shell prompt.

So either:
  1. That isn't the code you've successfully run from a shell prompt. It is
  2. Your code is failing before that point. not really It works well on the command line start and under WIng IDE
  3. There's something else you've not told us. um,yes, I am using Unix system on Windows (Sorry)
  4. All of the above
Oh, and shouldn't that be sys.exit() not exit()? exit() is valid python command.
Right the next thing I will do is make a very simple programme that write a line of text and see what happens there.

I want to get the cron thingy working, then move on to the systemd command.

User avatar
neilgl
Posts: 3242
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Cron is not starting my python MQTT script correctly

Wed Sep 23, 2020 6:09 pm

I am using Unix system on Windows
- so it is not really a raspberry pi related question?

User avatar
thagrol
Posts: 4916
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Cron is not starting my python MQTT script correctly

Wed Sep 23, 2020 10:25 pm

bonzadog wrote:
Wed Sep 23, 2020 5:47 pm
um,yes, I am using Unix system on Windows (Sorry)
So it's a windows related issue not a Pi one. Probably not even a python one.

Either way why on earth did you think it was apropriate to ask for help in the "using the raspberry Pi > troubleshooting" subforum?

If by "Unix sytem on Windows" what you actually meant was Raspberry Pi Desktop for PC and Mac running in a VM on windows you might have been better posting in that sub forum or at least telling us in your original post.

If you mean windows subsystem for linux, cygwin, or some other linux in a VM, you'd have been better of asking on their forums than here.

Stepping away from this thread now as I don't appreciate the way the OP has gone about things.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

User avatar
bonzadog
Posts: 272
Joined: Wed Apr 25, 2012 9:40 am
Location: Rietberg/Germany

Re: Cron is not starting my python MQTT script correctly

Thu Sep 24, 2020 10:12 am

thagrol wrote:
Wed Sep 23, 2020 10:25 pm
bonzadog wrote:
Wed Sep 23, 2020 5:47 pm
um,yes, I am using Unix system on Windows (Sorry)
So it's a windows related issue not a Pi one. Probably not even a python one.

Either way why on earth did you think it was apropriate to ask for help in the "using the raspberry Pi > troubleshooting" subforum?

If by "Unix system on Windows" what you actually meant was Raspberry Pi Desktop for PC and Mac running in a VM on windows you might have been better posting in that sub forum or at least telling us in your original post.

If you mean Windows subsystem for linux, cygwin, or some other linux in a VM, you'd have been better of asking on their forums than here.

Stepping away from this thread now as I don't appreciate the way the OP has gone about things.
No, it is indeed a Raspberry Pi problem. I was not clear in stating what I was doing. I started the programming under Windows ( I use WING IDE) THEN I move to the Raspberry Pi. Unfortunately I published the Windows version of my programme and not the Raspberry pi version. However, the only difference is the file name for the text file..

BUT when I tried the Rpi Programme on my RPi 4 8GB it does not run, HOWEVER when I moved to my RPi 4 4GB it works. So I'll generate a new SD and see what happens .

User avatar
B.Goode
Posts: 12051
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Cron is not starting my python MQTT script correctly

Thu Sep 24, 2020 10:21 am

bonzadog wrote:
Thu Sep 24, 2020 10:12 am

BUT when I tried the Rpi Programme on my RPi 4 8GB it does not run, HOWEVER when I moved to my RPi 4 4GB it works. So I'll generate a new SD and see what happens .



Someone is confused. I don't think it is me.

When you highjacked this old discussion a couple of days ago you said:
bonzadog wrote:
Tue Aug 25, 2020 2:30 pm
I have exactly the same problem.

Using a Raspberry Pi 3B using the OS BUSTER



a microSD card containing a copy of the RasPIOS Buster OS that runs on an RPi4B-4 will not necessarily run on an RPI4B-8 - the latter requires updated files on the boot medium that may not be present in earlier releases.

Restarting with a fresh installation of the current (August 2020) release of RasPiOS Buster is indeed a good next step.

User avatar
thagrol
Posts: 4916
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Cron is not starting my python MQTT script correctly

Thu Sep 24, 2020 10:48 am

bonzadog wrote:
Thu Sep 24, 2020 10:12 am
No, it is indeed a Raspberry Pi problem. I was not clear in stating what I was doing. I started the programming under Windows ( I use WING IDE) THEN I move to the Raspberry Pi. Unfortunately I published the Windows version of my programme and not the Raspberry pi version. However, the only difference is the file name for the text file..
Yet you stated "um,yes, I am using Unix system on Windows (Sorry)". I'm sorry but the above seems to me like a lame attempt to justify your poor behaviour after the fact.

Now definitely out of here.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

User avatar
bonzadog
Posts: 272
Joined: Wed Apr 25, 2012 9:40 am
Location: Rietberg/Germany

Re: Cron is not starting my python MQTT script correctly

Thu Sep 24, 2020 11:17 am

thagrol wrote:
Thu Sep 24, 2020 10:48 am
bonzadog wrote:
Thu Sep 24, 2020 10:12 am
No, it is indeed a Raspberry Pi problem. I was not clear in stating what I was doing. I started the programming under Windows ( I use WING IDE) THEN I move to the Raspberry Pi. Unfortunately I published the Windows version of my programme and not the Raspberry pi version. However, the only difference is the file name for the text file..
Yet you stated "um,yes, I am using Unix system on Windows (Sorry)". I'm sorry but the above seems to me like a lame attempt to justify your poor behaviour after the fact.

Now definitely out of here.
Why the necessary toxity? I see no bad behaviour - I was never rude , like you , and apologised for meine bad representation of the problem.

User avatar
neilgl
Posts: 3242
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near The National Museum of Computing

Re: Cron is not starting my python MQTT script correctly

Thu Sep 24, 2020 1:39 pm

Set Wait for network on boot using raspi-config. Basically it was looping around the connect, needed a break on successful connect. I called it bonza.py and start it at reboot from pi's cron with:

Code: Select all

crontab -e
@reboot python3 -u ~/forum/bonza.py >> ~/forum/bonza.log 2&1
I dropped the log file stuff from the python script. The mqtt (on another pi) published messages from a reboot are:

Code: Select all

mqtt-heartbeat Heartbeat0
mqtt-heartbeat Heartbeat1
mqtt-heartbeat Heartbeat2
mqtt-heartbeat Heartbeat3
the edited script bonza.py :

Code: Select all

# Improvement : coming up soon
# Status - cannot get this to start in CRON
# WORKS ON CLI   python3 /home/pi(Heartbeat.py
#
# Import package
import paho.mqtt.client as mqtt
import time
import datetime
import sys

# Status - cannot get this to start in CRON
# WORKS ON CLI   python3 /home/pi(Heartbeat.py

# Define Variables
MQTT_BROKER = "192.168.1.146"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "mqtt-heartbeat"
Connect_Flag=False
Counter = 0

# Define on_connect event Handler
# -------------------------------------------------------------------
def on_connect(mosq, obj, rc):
    print ("Connected to MQTT Broker")
# -------------------------------------------------------------------
# Define on_publish event Handler
def on_publish(client, userdata, mid):
    try:
        mqttc = mqtt.Client()
    except:
        print ("Error on Client - Broker probaly not available")
        sys.exit()

#####################################################################
##        Programme Start
#####################################################################

# Register Event Handlers
mqttc = mqtt.Client()
mqttc.on_publish = on_publish
mqttc.on_connect = on_connect

# Connect with MQTT Broker as a loop so it gets retried every 10 seconds.
while True:
    try:
        mqttc.connect(MQTT_BROKER, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
        print ("connected OK")
        Connect_Flag=True
        break
    except:
        t_connect= "Error on connecting to Broker : " + MQTT_BROKER + " at " + time.strftime('%Y-%m-%d %H:%M:%S')
        if Connect_Flag == False:
            print(t_connect+"\n")  # write error message
        time.sleep(10)             # sleep for 10 seconds and then retry forever
        continue
# ..... end of first while true .......

try_counter = 5
print ("starting heartbeat loop...")
while True:
# Test if a disconnection after a successful connect.

# Publish message to MQTT Topic
    time_value=time.strftime('{%Y-%m-%d %H:%M:%S}')
    MQTT_MSG = "Heartbeat"+str(Counter)
    Counter= Counter + 1
    try:
        mqttc.publish(MQTT_TOPIC,MQTT_MSG)
        print (MQTT_MSG)
    except :
        Error_mess= "ERROR in publish at " + time_Value
        print (Error_mess,MQTT_MSG)
        try_counter = try_counter - 1
        if try_counter == 0:
            break
    time.sleep(4)

# Disconnect from MQTT_Broker
mqttc.disconnect()
exit()

User avatar
bonzadog
Posts: 272
Joined: Wed Apr 25, 2012 9:40 am
Location: Rietberg/Germany

Re: Cron is not starting my python MQTT script correctly

Thu Sep 24, 2020 3:12 pm

neilgl wrote:
Thu Sep 24, 2020 1:39 pm
Set Wait for network on boot using raspi-config. Basically it was looping around the connect, needed a break on successful connect. I called it bonza.py and start it at reboot from pi's cron with:

Code: Select all

crontab -e
@reboot python3 -u ~/forum/bonza.py >> ~/forum/bonza.log 2&1
I dropped the log file stuff from the python script. The mqtt (on another pi) published messages from a reboot are:

Code: Select all

mqtt-heartbeat Heartbeat0
mqtt-heartbeat Heartbeat1
mqtt-heartbeat Heartbeat2
mqtt-heartbeat Heartbeat3
the edited script bonza.py :

Code: Select all

# Improvement : coming up soon
# Status - cannot get this to start in CRON
# WORKS ON CLI   python3 /home/pi(Heartbeat.py
#
# Import package
import paho.mqtt.client as mqtt
import time
import datetime
import sys

# Status - cannot get this to start in CRON
# WORKS ON CLI   python3 /home/pi(Heartbeat.py

# Define Variables
MQTT_BROKER = "192.168.1.146"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "mqtt-heartbeat"
Connect_Flag=False
Counter = 0

# Define on_connect event Handler
# -------------------------------------------------------------------
def on_connect(mosq, obj, rc):
    print ("Connected to MQTT Broker")
# -------------------------------------------------------------------
# Define on_publish event Handler
def on_publish(client, userdata, mid):
    try:
        mqttc = mqtt.Client()
    except:
        print ("Error on Client - Broker probaly not available")
        sys.exit()

#####################################################################
##        Programme Start
#####################################################################

# Register Event Handlers
mqttc = mqtt.Client()
mqttc.on_publish = on_publish
mqttc.on_connect = on_connect

# Connect with MQTT Broker as a loop so it gets retried every 10 seconds.
while True:
    try:
        mqttc.connect(MQTT_BROKER, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
        print ("connected OK")
        Connect_Flag=True
        break
    except:
        t_connect= "Error on connecting to Broker : " + MQTT_BROKER + " at " + time.strftime('%Y-%m-%d %H:%M:%S')
        if Connect_Flag == False:
            print(t_connect+"\n")  # write error message
        time.sleep(10)             # sleep for 10 seconds and then retry forever
        continue
# ..... end of first while true .......

try_counter = 5
print ("starting heartbeat loop...")
while True:
# Test if a disconnection after a successful connect.

# Publish message to MQTT Topic
    time_value=time.strftime('{%Y-%m-%d %H:%M:%S}')
    MQTT_MSG = "Heartbeat"+str(Counter)
    Counter= Counter + 1
    try:
        mqttc.publish(MQTT_TOPIC,MQTT_MSG)
        print (MQTT_MSG)
    except :
        Error_mess= "ERROR in publish at " + time_Value
        print (Error_mess,MQTT_MSG)
        try_counter = try_counter - 1
        if try_counter == 0:
            break
    time.sleep(4)

# Disconnect from MQTT_Broker
mqttc.disconnect()
exit()
Many thanks for your reply, I really appreciate your effort.

But is a break necessary in a try ? I though if the try was successful, the block would exit
anyway.

I tried to run on a different RPi 4 and it was showing that the programme was running ( using ps -aef )
The log file is something I need.

Again, thanks!

Return to “Troubleshooting”