2

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?

2 Answers 2

1

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.

0

use nohup, screen or tmux for backgrounding a process.

2
  • I tried to use ssh user@host2 'nohup /path/run |& tee myFile.txt' and ssh user@host2 'nohup /path/run 2>&1 myFile.txt' but it does not work. When I do ctrl+c after launching that, the process continues to run on host2, but the file does not record output anymore.
    – DevShark
    Commented Aug 15, 2016 at 9:29
  • 'nohup /path/run >> myFile.txt' Commented Aug 15, 2016 at 10:16

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