198

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?

4
  • 4
    A year late, but a good comprehensive answer was given here: stackoverflow.com/questions/4549489/…
    – Rahul Jha
    Oct 6, 2014 at 16:37
  • did you try nohup python file.py my_ouput.out &? Mar 5, 2021 at 14:34
  • can someone give an explanation of the command? can you explain what the hieroglyphics 2>&1 mean and why it works? Nov 12, 2022 at 2:01
  • 1
    @CharlieParker 2>&1 redirects stderr to stdout so when you use nohup myprogram > myprogram.out 2>&1 both stdout and stderr will be redirected to myprogram.out Nov 15, 2022 at 1:34

5 Answers 5

207

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)

7
  • 4
    And this isn't specific to GNU utilities, the behavior is specified by POSIX. Aug 20, 2012 at 22:33
  • 6
    It doesn't seem to work. It redirect the output of nohup command rather the output of myprogram.
    – Kris
    Mar 7, 2018 at 16:21
  • 6
    @Kris Redirection should take place before the trailing & character
    – yunzen
    Nov 7, 2018 at 8:29
  • does this also re-direct stderr? I want to direct everything, whether it is an error or output to a single source. Mar 5, 2021 at 14:13
  • Shouldn't it be nohup myprogram > myprogram.out 2>&1 & ? where adding & at the end
    – alper
    May 20, 2021 at 15:26
35

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

5
  • does this also re-direct stderr? I want to direct everything, whether it is an error or output to a single source. Mar 5, 2021 at 14:13
  • 2
    As described in the accepted answer, it needs to be nohup program > program.out 2>&1 & to also redirect stderr.
    – ncoghlan
    Mar 9, 2021 at 8:56
  • 1
    @ncoghlan can someone give an explanation of the command? can you explain what the hieroglyphics 2>&1 mean and why it works? Nov 12, 2022 at 2:00
  • @CharlieParker The four characters 2>&1 tells your shell that any output that usually would go to "file handle 2" (stderr) should be redirected to "file handle 1" (stdout).
    – svin83
    Nov 14, 2022 at 17:26
  • @CharlieParker The opposite also works. Example >&2 echo "Error!" would redirect the output of echo to stderr instead of stdout
    – svin83
    Nov 14, 2022 at 17:31
16

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.

3
  • @ncoghlan can someone give an explanation of the command? can you explain what the hieroglyphics 2>&1 mean and why it works? Nov 12, 2022 at 2:00
  • @CharlieParker those explanations are all over this page and not very closely related to nohup. It tells error output (notices about the operation of the program) to go to the same place standard output (the value-laden produce of the program) goes. The documentation is at gnu.org/software/bash/manual/html_node/Redirections.html Jul 17, 2023 at 12:48
  • 1
    @joarleymoraes Thanks so much. This was, in fact, making me crazy. Jul 17, 2023 at 12:49
3

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.

2
  • does this also re-direct stderr? I want to direct everything, whether it is an error or output to a single source. Mar 5, 2021 at 14:13
  • @ncoghlan can someone give an explanation of the command? can you explain what the hieroglyphics 2>&1 mean and why it works? Nov 12, 2022 at 2:01
1

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.
1
  • 1
    @CharlieParker Is this the kind of detail you hoped to find? Nov 16, 2022 at 1:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .