304

When I run nohup some_command &, the output goes to nohup.out; man nohup says to look at info nohup which in turn says:

If standard output is a terminal, the command's standard output is appended to the file 'nohup.out'; if that cannot be written to, it is appended to the file '$HOME/nohup.out'; and if that cannot be written to, the command is not run.

But if I already have one command using nohup with output going to /nohup.out and I want to run another, nohup command, can I redirect the output to nohup2.out?

2
  • do you know if the answers suggested also redirect the errors to the given location? Mar 5, 2021 at 14:15
  • I've personally found more useful to do nohup python script.py > my_output.out & Mar 5, 2021 at 14:33

6 Answers 6

514
nohup some_command &> nohup2.out &

and voila.


Older syntax for Bash version < 4:

nohup some_command > nohup2.out 2>&1 &
14
  • 5
    @ismail, if I understand, this directs the output away from stdout and nohup.out, into nohup2.out. Then what does 2>&1& do? Dec 28, 2010 at 21:25
  • 13
    2>&1 redirects stderr to the same output file as stdout which is in this case nohup2.out
    – ismail
    Dec 28, 2010 at 21:28
  • 20
    In Bash 4, the two redirects can be abbreviated as one &> nohup2.out.
    – ephemient
    Dec 28, 2010 at 21:41
  • 1
    I tried &> nohup2.out and it works, but how can you tell your bash version?
    – monkut
    Aug 8, 2012 at 7:44
  • 1
    @monkut Just typing help in bash also works. Or help | grep version. Easier to remember to me.
    – Godsmith
    May 14, 2014 at 14:01
69

For some reason, the above answer did not work for me; I did not return to the command prompt after running it as I expected with the trailing &. Instead, I simply tried with

nohup some_command > nohup2.out&

and it works just as I want it to. Leaving this here in case someone else is in the same situation. Running Bash 4.3.8 for reference.

1
  • 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:14
19

Above methods will remove your output file data whenever you run above nohup command.

To Append output in user defined file you can use >> in nohup command.

nohup your_command >> filename.out &

This command will append all output in your file without removing old data.

1
  • 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:15
12

As the file handlers points to i-nodes (which are stored independently from file names) on Linux/Unix systems You can rename the default nohup.out to any other filename any time after starting nohup something&. So also one could do the following:

$ nohup something&
$ mv nohup.out nohup2.out
$ nohup something2&

Now something adds lines to nohup2.out and something2 to nohup.out.

2
1

my start.sh file:

#/bin/bash

nohup forever -c php artisan your:command >>storage/logs/yourcommand.log 2>&1 &

There is one important thing only. FIRST COMMAND MUST BE "nohup", second command must be "forever" and "-c" parameter is forever's param, "2>&1 &" area is for "nohup". After running this line then you can logout from your terminal, relogin and run "forever restartall" voilaa... You can restart and you can be sure that if script halts then forever will restart it.

I <3 forever

1
  • 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:14
0

To send the output of a nohup command to a custom file name, you can use the redirection operator > to specify the file name. Here's how you can do it:

  1. Open your terminal or command prompt.

  2. Run the nohup command followed by the command you want to execute, and redirect the output to the desired file name using the > operator. For example:

   nohup your-command > custom-file-name.log &

Replace your-command with the actual command you want to execute in the background, and custom-file-name.log with the desired file name for the output log.

  1. Press Enter to execute the command. The output of the command will be redirected to the specified file name.

  2. You can now close the terminal or command prompt without stopping the execution of the command. The output will continue to be written to the specified file.

Note: If you want to append the output to an existing file instead of creating a new one, you can use >> instead of > in the redirection operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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