Is there a way to redirect nohup output to a log file other than nohup.out?

I frequently use the program nohup so that my processes are immune to hangups. So if I want to make the program program immune to hangups, I use the command

nohup program &

where & puts the process in the background.

When starting, nohup gives me the message:

nohup: appending output to `nohup.out’

Is there any way to send the output to a file other than nohup.out? Often I want to run many processes in the same directory using nohup, but if I do this, all the output gets lumped together in a single nohup.out file.

The manual page (for example, here) does not seem to have an option for specifying the log file. Can you please confirm this? Also, do you have any thoughts of how I can work around this problem?

Asked By: Andrew

||

GNU coreutils nohup man page indicates that you can use normal redirection:

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.

Edit: I didn’t read your link at first; you may have a different version of nohup, although this section suggests that you can still use normal redirection:

 nohup.out          The output file of the nohup execution if
                    standard  output is a terminal and if the
                    current directory is writable.

You can redirect standard output and standard error to different files:

nohup myprogram > myprogram.out 2> myprogram.err &

or to the same file:

nohup myprogram > myprogram.out 2>&1 &

(don’t forget the & at the end to put into the background)

Answered By: lk-

Adding Jim comment as an answer here for more visibility.

use nohup program > program.out & to write the output to program.out instead of nohup.out

Answered By: mrsrinivas

nohup program &> program.out &

or

nohup program &> program.out if you don’t want to run the job in the background. I use this when I’m running multiple jobs at once and I want to limit it.

Answered By: Cheryl Grant

Ok, if you are using python and have been searching for a long time, and couldn’t get it working. Here you go:

nohup python -u my_script.py > program.out 2>&1 &

Reason: python buffers before writing to the output, -u disables that behavior.

Update: you can also set env. var. PYTHONUNBUFFERED=1 for the same behavior.

Answered By: joarleymoraes

Can someone give an explanation of the command? can you explain what the hieroglyphics 2>&1 mean and why it works?

  • The nohup command executes the command give while ignoring SIGHUP.
  • The SIGHUP stands for "hang-up", which is typically sent to a program when you lose connection to the system (network disruption.)
  • If the standard output is a terminal, it defaults to writing to nohup.out in the current directory.
  • The 2>&1 invalidates the "connected to a terminal" part.
  • The 2> means standard error is written to &1, which means the same as standard output.
  • Then the > program.out tells the shell to write the standard output to the file program.out.
Answered By: James Risner
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.