Zigbee2mqtt closes when I close putty window

This might not be the right place to ask this but hopefully, someone can help me.

I have set up Eclipse Mosquitto in docker and Zigbee2MQTT on a Raspberry Pi 3b+. When I start Zigbee2MQTT it successfully connects to the MQTT server and I can connect a device and see it in Home Assistant. The problem is that as soon as I close the SSH session in Putty to the Raspberry Pi the connection to the MQTT server also close and the device becomes unavailable in Home Assistant.

What is the proper way to close Putty and still leave the connection to the MQTT server open?

It sounds like you are running your docker container using “docker run” and when you exit your putty session, it ends the docker process you are running in your putty session. If that’s the case, you need to add the -d switch to your docker run command, and it will run in detached mode, similar to a service.

I followed this guide on youtube

sudo docker run --name=mqtt -itd --restart=always --net=host 
-v /volume1/docker/mqtt/config:/mosquitto/config -v /volume1/docker/mqtt/data:/mosquitto/data 
-v /volume1/docker/mqtt/log:/mosquitto/log 
-v /volume1/docker/mqtt/config/mosquitto.conf:/mosquitto/config/mosquitto.conf 

I tried setting -d after run in the command but it made no difference.

I am not sure, when I run exactly what you have above, it runs the container detached for me. It should only output the container id after you run it with -d:

chad@docker:~# sudo docker run --name=mqtt -itd --restart=always --net=host -v /srv/docker/mqtt/config:/mosquitto/config -v /srv/docker/mqtt/data:/mosquitto/data -v /srv/docker/mqtt/log:/mosquitto/log -v /srv/docker/mqtt/config/mosquitto.conf:/mosquitto/config/mosquitto.conf:rw eclipse-mosquitto:openssl

This means that it’s running in the background (detached) and you can safely close your tty session without disrupting the mqtt container.

What are you seeing when you run the command you pasted?

The docker container seems to run fine. To clarify the Putty connection is to the Raspberry Pi running Zigbee2MQTT.

Here’s an excerpt from the docker log

1670084619: mosquitto version 2.0.15 starting
1670084619: Config loaded from /mosquitto/config/mosquitto.conf.
1670084619: Opening ipv4 listen socket on port 1883.
1670084619: mosquitto version 2.0.15 running
1670084648: New connection from on port 1883.
1670084648: New client connected from as 0B3bYGz67E6hzIt5Mydtqj (p2, c1, k60).
1670084768: New connection from on port 1883.
1670084768: New client connected from as mqttjs_8f4a2e84 (p2, c1, k60).
1670084857: Client mqttjs_8f4a2e84 closed its connection.
1670085017: New connection from on port 1883.
1670085017: New client connected from as mqttjs_2801c57c (p2, c1, k60).
1670086420: Saving in-memory database to /mosquitto/data/mosquitto.db.
1670088221: Saving in-memory database to /mosquitto/data/mosquitto.db.
1670090022: Saving in-memory database to /mosquitto/data/mosquitto.db.
1670090131: Client mqttjs_2801c57c closed its connection. is the Raspberry Pi is Home Assistant

Ah, I see, sorry for the misunderstanding!

Is Zigbee2MQTT running as a service on your Pi, or are you running the process from your putty session?

Does this command show that your zigbee2mqtt is running as a service, on your Pi?

sudo systemctl status zigbee2mqtt

If you get something like “Unit zigbee2mqtt.service could not be found.” then it is not enabled as a service and you will need to configure it. Here’s a good link I found off the top on how to configure the zigbee2mqtt service and set it to start on boot: How to set up Zigbee2MQTT on a Raspberry Pi and integrate it with Home-Assistant

Thanks, that seems to do the trick!

1 Like

Great! Glad it’s working for you now!