I send a tee
command from host 1 to host 2:
ssh user@host2 '/path/run |& tee myFile.txt'
I use tee
so that I get the output of the binary to be added to myFile.txt
The problem I have then is after a bit of time, I want to regain control of my local host without having a lot of printout. So I do CTRL+C. This lets the process on host2 continue to run, which is what I want, but it stops the tee
process itself, so the file is not populated.
I tried to replace |& tee myFile.txt'
by 2>&1 myFile.txt' &
but it did not help.
How can I ensure that the file continues to be populated on host2, while regaining control to my session on host1?
If you want to record the results in some file (work with IO redirection inside of the nohup
), you need to enclose all the pipeline in the nohup
. It does not use shell expansions, since argument is only COMMAND
ARGS
, so using a sh
is a good way:
ssh user@host2 'nohup sh -c "/path/run |& tee myFile.txt" &'
but note that nohup
will disconnect the terminal from the command ant it might fail. Useful would be to redirect it directly to the file:
ssh user@host2 'nohup sh -c "/path/run &> myFile.txt" &'
Inspiration from the SO answer.