Mosquitto reconnect after power outage on broker?
Bo Berglund
bo.berglund at gmail.com
Tue Apr 23 13:07:21 UTC 2024
On Tue, 23 Apr 2024 10:33:40 +0100, Colin Law <clanlaw at gmail.com> wrote:
>On Tue, 23 Apr 2024 at 10:09, Bo Berglund <bo.berglund at gmail.com> wrote:
>>
>> I have a script running on boot on an Ubuntu server 20.04 LTS and its purpose is
>> to log all activity on an MQTT broker. The script command is this (newsreader
>> does word wrap..):
>>
>> CMD="mosquitto_sub -h ${BROKERIP} -i ${MQTTID} -F '@Y- at m-@d @H:@M:@S ; %t ; %p'
>> -t '#' >> ${FULLOG}"
>> ...
>> eval "$CMD"
>>
>> It works well so far, but I am wondering if mosquitto will reconnect if the
>> broker system experiences a power outage and is restarted when power returns?
>
>You can do at least a partial test by disconnecting your computer from
>the network for a few minutes and see if that recovers.
>I suspect that mosquitto_sub will shut down with an error.
>
>A better way to do it might be with a systemd script, then you can
>control the restart on failure behaviour.
>
Yes, I could test but I must then simulate the shutdown of the broker device...
It is at a remote location accessed via OpenVPN between the routers (both
routers are on fiber).
The event I am worried about is the power down on the remote site, which will
bring down all of the computers there as well as the router and the fiber
connection box. Unfortunately I don't have a battery backup system on that place
so everything breaks down when power is removed.
When power returns then the fiber box and the router as well as all of the
devices start up including the MQTT broker (an RPi4 device). Once the router is
running it autoconnects to my home LAN and from then on any MQTT call from the
home server will be acted on.
But my worry is the script that starts on boot on my Ubuntu server and
subscribes to # (everything) on the broker. Since that script has not been
interrupted since the last start at home on July 7th 2023 and still runs I guess
it must have survived on power drops at the remote site, provided there has been
any.
I remember many outages last spring but from July maybe not so much...
So does mosquitto-sub have a built-in failover that lets it handle a
disconnection and then reconnect as soon as possible and resume operations?
I just checked the broker RPi4 and it says it has had an uptime of 4 days 5h18m,
which means that the broker has restarted and mosquitto_sub handled it
apparently without disruption. :)
Of course that did not necessarily be caused by a total power outage like in my
question...
But it looks pretty resilient anyway (given that a restart normally just lasts a
minute or so)...
On Thursday there will be installations done at the remote site and it probably
needs a shutdown of electricity then. So I get to see how it actually works.
Keep my fingers crossed.
--
Bo Berglund
Developer in Sweden
More information about the ubuntu-users
mailing list