I can use mosquitto_sub to subscribe to broker for a specific topic, say test_topic,

mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic

I can also pipe grep for a specific keyword, say valve_closed,

mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic | grep "valve_closed"

This would only output a newline with "valve_closed", every time that particular topic broadcasts "valve closed" message.


Line 1: mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic | grep "valve_open"

Line 2: mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic | grep "valve_closed"


Line 1: valve_closed

Now, my question is, how can I pipe a command such that if grep "valve_closed" is true, then execute another command.

I've tried chaining a command like echo "Okay so now Valve is Closed" with another | pipe command,

mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic | grep "valve_closed" | echo "Okay, so now Valve is Closed"

But this simply runs the echo command with output "Okay, so now Valve is Closed", even when I group grep+echo within parentheses.

I also tried using AND operator &&,

mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic | grep "valve_closed" && echo "Okay, so now Valve is Closed"

While this waits for grep to match string valve_closed as soon as it finds a match, it would execute echo "Okay, so now Valve is Closed".

I've also tried using if statement, however any inclusion of conditional statement seems to break the polling nature of message_sub which constantly monitors a given topic and prints out its output.

I'm kind of lost as to how I can use mosquitto_sub to retrieve messages and execute shell commands/scripts based on message content. Thanks for reading.

  • 1
    xargs should work for you for this. Research using xargs with grep. Come back and ask if you were not able to figure it out. Commented May 29, 2021 at 22:23

2 Answers 2

mosquitto_sub -h MOSQUITTO_BROKER_HOSTNAME -t test_topic | grep "valve_open" | some_other_script_to_handle_the_output.sh

#content of some_other_script_to_handle_the_output.sh example script
echo "Input parameter: $1"
exit 0

That's one of the beautiful things about the pipe "|". They can be chained together.

That other script could effect changes elsewhere in a myriad of ways. Based on the frequency of these MQTT responses, you would want that script to finish quickly so things don't get backed up, but usually that's not a problem. Trial and error is your friend here and could lead you to a more efficient methodology down the line.


The problem here is that mosquitto_sub never returns (by default) so it's output never ends so can't be used to test against.

You will do MUCH better not trying to do this in a shell script and use something like python.


If you REALLY MUST do this without writing something (trust me it is worth it) then you can use mqtt-exec (though it would require nodejs installing)


  • If you design testtopic to only have trigger messages (ie. messages that you want to act on), then use -C 1 to exit on any message received. Else, if you still insist on mosquitto_sub, then put it in a loop, at the risk of missing messages... Commented May 29, 2021 at 14:25
  • Thanks. I tried it in one of my earlier iterations and guess what - there's an inherent delay between the time a message is received and mosquitto_sub realizing it's fulfilled -C 1 condition. Sometimes the order of that delay is upwards of a second, which defeats purpose of low latency nature of MQTT broadcast messages.
    – user14252
    Commented May 29, 2021 at 16:07
  • Well, looks like user error because when I tried the minimal reproduction against a local broker, the mosquitto_sub exited immediately after a mosquitto_pub, unless you have long latency between your clients and your broker, then that's an entirely new problem. You will not solve your problem unless you are meticulous and precise. Commented May 30, 2021 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.