16

Is it possible to let a process started with nohup, write to stdout instead of in a file? or maybe in the file and on the screen?

nohup python start.py &

writes to nohup.out, but I'd like it to output to the screen, nohup is only meant as a hedge when the connection breaks.

3 Answers3

10

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.

dsh
  • 303
  • 1
  • 6
  • 1
    One nice addition: If the nohup command was already started with a file name, then you can use `tail -f filename` to see what is written to the file. – Hennes Aug 04 '12 at 22:16
  • Doesn't work, once the connection stops the file is not written anymore – Davoud Taghawi-Nejad Aug 05 '12 at 11:32
  • You're right: I answered the question (send output to terminal) but defeated the purpose. I rewrote my answer accordingly. – dsh Aug 05 '12 at 16:57
6

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.
Henk Langeveld
  • 1,294
  • 10
  • 25
-1
vladimir@lin-mint-cin:~$ tty<br>
/dev/pts/8<br>
vladimir@lin-mint-cin:~$ ln -sf /dev/pts/8 nohup.out<br>
vladimir@lin-mint-cin:~$ nohup ls<br>
nohup: ввод игнорируется, вывод добавляется в 'nohup.out'<br>
2019-05-31-090159.jpg    giphy.gif        nim_porojects   sketchbook
Aptana_Studio_Workspace  IdeaProjects         nohup.out       target
Desktop          jack_capture_01.wav  Pictures        Templates
Documents        lib              Postman         tmp
Downloads        mad              Projects        trace.edn
eclipse          Mail             prolog_ws       uinfild_hill-iskusstvo_shemotehniki_tom_1_izd4-e-1489315100.pdf
eclipse-dsl-workspace    Music            proxy-secret    Videos
eclipse-workspace    mygolo           Public          workspace-dltk
erlang_ex        my_golo_ws       rust-workspace  проекты
Machavity
  • 834
  • 10
  • 26