linuxterminalio-redirectiontee

Linux terminal redirect output of piped commands to terminal and file


While this topic seems a duplication of queries such as How to redirect output to a file and stdout, I cannot make my command sequence to produce the desired result.

This is the command sequence that works:

mosquitto_sub -h 192.168.1.5 -v -t ArgyleCourt/Property/# | xargs -d$'\n' -L1 sh -c 'date "+%Y-%m-%d %T.%3N $0"' | grep "IrrigationTank\|BoreP\|Hub"

... and produces the desired result: 2024-12-25 10:54:17.255 ArgyleCourt/Property/IrrigationTank/WaterLevel 84 2024-12-25 10:55:17.254 ArgyleCourt/Property/IrrigationTank/WaterLevel 83 2024-12-25 10:55:35.243 ArgyleCourt/Property/BorePump/HeartBeat ON 2024-12-25 10:55:35.308 ArgyleCourt/Property/Hub/HeartBeat ON 2024-12-25 10:56:17.253 ArgyleCourt/Property/IrrigationTank/WaterLevel 84 2024-12-25 10:56:33.479 ArgyleCourt/Property/Hub/Notification Arduino_TheHub|INFO|DHCP OK 2024-12-25 10:57:17.251 ArgyleCourt/Property/IrrigationTank/WaterLevel 84 2024-12-25 10:57:35.234 ArgyleCourt/Property/BorePump/HeartBeat ON 2024-12-25 10:57:35.295 ArgyleCourt/Property/Hub/HeartBeat ON 2024-12-25 10:57:52.391 ArgyleCourt/Property/Hub/ChipTemp 45

This command line uses a mosquitto (MQTT broker) command to query the broker for the specific topic ArgyleCourt/Property/#, piped to args to add a date/time stamp and piped to grep to filter out the messages for three controllers (TheHub, IrrigationTank and BorePump).

According to the linked query above, this:

(mosquitto_sub -h 192.168.1.5 -v -t ArgyleCourt/Property/# | xargs -d$'\n' -L1 sh -c 'date "+%Y-%m-%d %T.%3N $0"' | grep "IrrigationTank\|BoreP\|Hub") 2>&1 | tee MQTT_TankfillMsgs.txt

... it should have the output in the terminal and the file. The file is created, but never written to. The () are used to capture/group the whole command sequence.

Another response suggested to use unbuffer. I added this command before mosquitto_sub, but it made no difference to the outcome.

What am I missing?


Solution

  • I eventually figured it out:

    mosquitto_sub -h 192.168.1.5 -v -t ArgyleCourt/Property/# | xargs -d$'\n' -L1 sh -c 'date "+%Y-%m-%d %T.%3N $0"' | grep --line-buffered "IrrigationTank\|BoreP\|Hub" | tee MQTT_TankfillMsgs.txt
    

    ... adding -line-buffered to the grep command will output the filtered MQTT input to both, file and terminal.

    Thanks for hanging in there.