Is broker ready to accept connection - Linux

I have several services starting along with hiveMQ.
Because systemd starts them all at the same time I need some way of detecting that the broker is ready to receive a connection.
I have set the “Requires” option in my service units but at boot up I regularly see connection refused from my client services causing each service to restart.
I have also tried a sleep with ExecStartPre=/bin/sleep 60 but this is not too efficient.

What is the best way to test on Linux using Python to see if the broker is ready. (does it produce a file I can test for for example)

Many thanks for any help… Mike

Hivemq running on Raspberry Pi 4 along with services requiring access so all local.

Hi @watsonm and welcome to our MQTT community!

In order to see if the broker is ready I would check if its listeners are open. In the hivemq.log there should be a line like this:
INFO - Started TCP Listener on address 0.0.0.0 and on port 1883.
and also you will “see” the port in your Linux:
lsof -Pi @localhost:1883 -sTCP:LISTEN -t >/dev/null

I hope this helps.
Kind regards,
Dasha

1 Like

Daria,
Many thanks … worked a treat …

while True:
if os.system(“grep -rnw /home/pi/hivemq/log/ -e ‘1883’”) == 0:
break

in CheckHivemq.py file and

ExecStartPre=/usr/bin/python /home/pi/scripts/CheckHivemq.py

in the service file…

Also add
ExecStartPre=/usr/bin/rm /home/pi/hivemq/log/hivemq.log
to hivemq service file!!

Thanks again Mike

Hi @watsonm ,

Looks good, just one more note: in the hivemq.log, there will be first the line, when the listener is only starting:

INFO - Starting TCP Listener on address 0.0.0.0 and on port 1883.

and, if everything is good, then there will be the line that the listener HAS started:

INFO - Started TCP Listener on address 0.0.0.0 and on port 1883.

So you might want to “tune” your Python script to differentiate between the two.

Good luck with your project!
Dasha from HiveMQ team

Daria thanks ,

I had appreciated that but just wanted to see if that would work as HiveMQ itself has been stable.

As a retired assembler and Windows programmer (ex IBM ) my LInux/Python skills are not at their best!!

grep -nw /home/pi/hivemq/log/hivemq.log -e ‘Started TCP Listener’ | grep -nw -e ‘1883’

Many thanks MIke