From man nohup
:
If standard input is a terminal, redirect it from /dev/null. If standard output is a terminal, append output to nohup.out
if possible, $HOME/nohup.out
otherwise. If standard error is a terminal, redirect it to standard output. To save output to FILE, use nohup COMMAND > FILE
.
(completely revised based on comments )
When exiting a shell or closing a terminal, the child processes will be sent SIGHUP (hangup, the name originates from the time when terminals would dial in to the UNIX computer) to tell them that the terminal is no longer connected. Additionally, when the shell process ends, the child process's stdin, stdout, and stderr streams will be closed. Usually this causes the child process to die. Usually this is a good thing because it avoids leaving processes running with no user.
The purpose of the nohup
program is to keep the process running even after the shell terminates and the terminal disconnects. As such, directing its output to the terminal completely defeats the purpose. My original answer (nohup COMMAND | cat
) was not helpful because: when the terminal is closed, cat's output is closed and so cat dies which breaks the pipe and sends a SIGPIPE to the nohup process causing it to die. I had answered the question without thinking of the purpose.
To achieve your purpose, run nohup COMMAND > FILE
to select the name of the file to direct output to. Then use tail -f FILE
to watch the output on a terminal.
Similarly, one could use the following shell construct: COMMAND >FILE 2>&1 </dev/null &
to run a command whose stdio streams are not connected to the terminal and can continue running after the terminal is closed. Additionally, in zsh, run the shell-builtin disown
to tell zsh to leave the process instead of killing it when the shell ends.