Redirect nohup to stdout

Solution 1:

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.

Solution 2:

You can achieve this with a separate process to inspect the file, or by using a terminal multiplexer like screen or tmux.

  1. nohup python start.py & tail -f nohup.out

The tail -f can be killed and restarted at will, without affecting the python process.

  1. screen python start.py and dis-/re-connect at will.

Tags:

Nohup