Mosquitto MQTT Broker:连接外部代理、订阅主题及断连重连脚本实现问询
Hey there! Let's break down your two Mosquitto needs step by step—super straightforward once you know the tricks.
1. Connecting Your Local Mosquitto Broker to an External Proxy & Subscribing to External Topics
To pull in topics from an external MQTT broker, you'll want to set up a Mosquitto bridge—this lets your local broker mirror topics from the external one (or even sync both ways). Here's how to do it:
- First, open your Mosquitto config directory (usually
/etc/mosquitto/conf.d/) and create a new bridge config file, likeexternal-bridge.conf. - Add these lines (replace the placeholders with your external broker's details):
connection external-proxy-bridge address external-broker-host:1883 # Replace with actual host:port # Uncomment these if the external broker requires auth # remote_username your-external-username # remote_password your-external-password # Define topic bridging: format is <local-topic> <remote-topic> <direction> # Use "in" to pull external messages to local, "out" to send local to external, "both" for bidirectional topic # in # This subscribes to ALL external topics and forwards them to your local broker # Or use a specific topic if you don't need everything: # topic my/local/topic in my/external/topic - Save the file, then restart Mosquitto to apply the changes:
sudo systemctl restart mosquitto - Once the bridge is up, you can subscribe to the mirrored topics on your local broker just like you normally would. For example, if you used the
# inrule, runningmosquitto_sub -V mqttv311 -t "#"on your local machine will show all messages from the external broker.
2. Bash Script to Monitor Subscription & Auto-Reconnect
While mosquitto_sub has basic reconnection logic, a custom script gives you more control over monitoring and restarting. Here's a robust bash script that will keep your subscription alive:
#!/bin/bash # --- CONFIGURATION --- MQTT_BROKER="localhost" MQTT_TOPIC="your-target-topic" # Replace with your actual topic MQTT_VERSION="mqttv311" EXTRA_FLAGS="" # Add flags like "-u your-user -P your-pass" if needed CHECK_INTERVAL=5 # Seconds between connection checks LOG_FILE="/var/log/mqtt_sub_monitor.log" # Optional: log to a file # --------------------- start_sub() { echo "$(date): Starting subscription to $MQTT_TOPIC on $MQTT_BROKER..." | tee -a $LOG_FILE # Launch the subscription in the background, redirect output to log mosquitto_sub -V $MQTT_VERSION -h $MQTT_BROKER -t $MQTT_TOPIC $EXTRA_FLAGS >> $LOG_FILE 2>&1 & SUB_PID=$! echo "$(date): Subscription started with PID $SUB_PID" | tee -a $LOG_FILE } # Start the subscription initially start_sub # Monitoring loop while true; do # Check if the subscription process is still running if ! kill -0 $SUB_PID 2>/dev/null; then echo "$(date): Subscription lost! Restarting..." | tee -a $LOG_FILE start_sub fi sleep $CHECK_INTERVAL done
How to use this script:
- Replace the configuration values with your own (broker address, topic, auth flags if needed).
- Make the script executable:
chmod +x mqtt_auto_reconnect.sh - Run it in the background so it keeps working even if you log out:
nohup ./mqtt_auto_reconnect.sh & - For even better persistence, you can set it up as a
systemdservice—this way it starts automatically on boot and restarts if your server reboots.
Extra tips:
- The
LOG_FILEline lets you track when the subscription starts/restarts—super helpful for debugging. - Adjust
CHECK_INTERVALto make the checks more or less frequent (5 seconds is a good middle ground).
内容的提问来源于stack exchange,提问作者2437850
相关产品推荐
相关产品推荐

