1

I am trying to work with a remote device that I login to using SSH, the device uses MQTT for communication, and I want to be able to log that remotely without keeping a terminal open with the command running. Currently my command I run is shown below, but it cannot be ran without keeping a laptop connected with the terminal running.

I have read about using solutions like (nohup "command" &), but I have been unable to factor that into the below code.

mosquitto_sub -v -u Test -P Test123 -t '#' | while read -r line ; do
        echo -e "$(date "+%Y-%m-%d %H:%M:%S") $line"
done | tee -a MQTT.txt

So to summarize I want to be able to run the above code, and detach the process from the terminal so that it will continue to run without the terminal being connected, which I believe is possible using nohup, but I also still need to log the messages to a local file so they can be reviewed later. How would I change the above code so that it is possible to do that?

  • You could use screen. – Peschke Apr 1 at 21:35
1

You can write

nohup bash -c 'mosquitto_sub -v -u Test -P Test123 -t "#" | while read -r line ; do
        echo -e "$(date "+%Y-%m-%d %H:%M:%S") $line"
done | tee -a MQTT.txt'

However what you really want is screen or tmux. These program will allow you to run the command, logout and after login just connect to old "already closed" terminal.


Very basic screen session in your example will look like this:

screen

mosquitto_sub -v -u Test -P Test123 -t '#' | while read -r line ; do
    echo -e "$(date "+%Y-%m-%d %H:%M:%S") $line"
done | tee -a MQTT.txt

# Hit "Ctrl-a d", it will detach the screen

Now you can close the terminal and logout, after login again run screen -R.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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