I'm trying to create a .txt file from a redirect from the mosquitto_sub command. I want the file to be overwritten each time it receives new data from MQTT. This doesn't work:
mosquitto_sub -h 192.168.1.10 -t "application/7/device/a8404117b18312e9/rx" > newtest.txt
This appends output at each new data received. I only need the latest in the file. I'm not a programmer so there may be something simple I'm missing. Thanks in advance BG
The best you a probably do here is to add the -C
flag set to 1 which will have the client exit after the first message and place the command in a loop in a shell script.
This does have the possibility to miss messages if the rate of publication is high enough.
You will also need to use a temporary file for the redirect as it will zero out the target of the redirect as soon as it starts.
e.g.
#!/bin/bash
while true; do
mosquitto_sub -C 1 -h 192.168.1.10 -t "application/7/device/a8404117b18312e9/rx" > temp.txt
mv temp.txt newtest.txt
done