2

If you redirect the nohup application as:

nohup bash -c "printf \"command\n\"" &> /dev/null

The nohup.out file is not created, however the terminal I ran the command also do not get any output. How to keep the terminal output from the command but not create the nohup.out file?

  • 2
    What exactly are you trying to accomplish? The point of nohup is to make the command run independently of the current terminal session (especially, to be able to keep running after the session ends). But if its output is going to the current terminal, and you close the terminal... how is it supposed to continue? What's supposed to happen in that case? – Gordon Davisson Jul 28 '17 at 6:58
  • Then I just loose the command output, until I externally close the application and open it again with another terminal. – user Jul 28 '17 at 15:17
  • The command's output has to go somewhere. It could go to a terminal (tty device), to a file, to /dev/null, to a pipe being read by another program... but it has to go somewhere, and there's no mechanism for it to switch over (e.g. from the terminal to /dev/null when the terminal vanishes). I suppose you could write a program that'd copy from a pipe to a terminal until the terminal closes, then just discard input, but you'd have to write that program yourself. – Gordon Davisson Jul 28 '17 at 20:59
2

Try this,

To get the STDOUT and STDERR both on console and in a nohup.out file, execute the following before executing your nohup command.

exec >  >(awk '{ print $0; fflush();}' | tee -a nohup.out)
exec 2>  >(awk '{ print $0; fflush();}' | tee -a nohup.out >&2)
nohup bash -c "printf \"command\n\"" &

EDIT:

If you want the nohup.out to not be created, then try this

exec >  >(awk '{ print $0; fflush();}')
exec 2>  >(awk '{ print $0; fflush();}')
nohup bash -c "printf \"command\n\"" &

This will not create the nohup.out while displaying STDOUT and STDERR on the console.

Put the above lines in a script and then run it in the background.

  • I only want to nohup output on the console. I do not want it creating a file. – user Jul 28 '17 at 15:18
  • @user, Edited the answer as per your comment. – sai sasanka Jul 31 '17 at 9:21
1

You can run it as follows

nohup process & tail -f nohup.out

Or use tee , which is util to do exactly what you need: read from standard input and write to standard output and files

nohup process 1>&2 | tee nohup.out &

  • second give me nohup: ignoring input and appending output to 'nohup.out' – Archemar Jul 28 '17 at 10:05
  • I only want to nohup output on the console. I do not want it creating a file. – user Jul 28 '17 at 15:18
  • From the man page: "nohup - run a command immune to hangups, with output to a non-tty". What are you trying to achieve exactly? You might need to use a different implementation – Chen A. Jul 28 '17 at 19:02
  • I am trying to keep the program running and let the terminal free to run commands while printing the program's output, but when I close the terminal I would like to not close the program – user Jul 28 '17 at 22:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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