303

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?

David LeBauer
  • 31,011
  • 31
  • 115
  • 189

5 Answers5

513
nohup some_command &> nohup2.out &

and voila.


Older syntax for Bash version < 4:

nohup some_command > nohup2.out 2>&1 &
ismail
  • 46,010
  • 9
  • 86
  • 95
  • 5
    @ismail, if I understand, this directs the output away from stdout and nohup.out, into nohup2.out. Then what does `2>&1&` do? – David LeBauer Dec 28 '10 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 '10 at 21:28
  • 20
    In Bash 4, the two redirects can be abbreviated as one `&> nohup2.out`. – ephemient Dec 28 '10 at 21:41
  • 1
    I tried &> nohup2.out and it works, but how can you tell your bash version? – monkut Aug 08 '12 at 07:44
  • I found that this redirect method always rewrite out file, not the standard append writing way. How to achieve redirect while not rewrite the out file? – user15964 Nov 17 '13 at 02:02
  • With `GNU bash, version 4.3.8(1)-release (x86_64-pc-linux-gnu)` the `&>>` version is not working for me. (on `Ubuntu 14.04 LTS`) – Vajk Hermecz May 05 '14 at 16:31
  • @VajkHermecz there is only one `>` so it is `&>` – ismail May 06 '14 at 12:37
  • 1
    @monkut Just typing `help` in bash also works. Or `help | grep version`. Easier to remember to me. – Godsmith May 14 '14 at 14:01
  • 1
    I think, what this does actually is not redirect the output of `nohup`, but the output of the `nohup`ped command. The result maybe not the same, as `nohup` creates the `nohup.out` anyway. It's just empty in this way. – yunzen Feb 02 '17 at 09:59
  • @ismail, what is the need to add '&' at the end of the command? I see commands everywhere over the net where there is no ampersand at the end of the command. I am confused. Can you explain why? – Djeah Apr 02 '18 at 19:17
  • @Djeah, I had the same question as to why there are both ampersands. the `&>` operator redirects to stdout (http://www.gnu.org/software/bash/manual/bash.html#Redirections) and the second `&` just puts the job in the background, though you can also Ctrl+Z and bg and achieve that result. – jimh Feb 13 '20 at 18:49
  • when I do `nohup command > file.out 2>&1` it works, but it's not running in the background. When I add the `&` at the end, it runs it the background, but I get no data recorded in file.out. How do I fix this? – Daniel C Jacobs May 13 '20 at 15:31
  • does this also re-direct stderr? I want to direct everything, whether it is an error or output to a single source. – Charlie Parker Mar 05 '21 at 14:14
  • @CharlieParker Yes, file descriptor '2' is for stderr. – ismail Mar 08 '21 at 16:23
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.

Godsmith
  • 2,492
  • 30
  • 26
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.

Irshad Khan
  • 5,670
  • 2
  • 44
  • 39
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.

TrueY
  • 7,360
  • 1
  • 41
  • 46
  • does this also re-direct stderr? I want to direct everything, whether it is an error or output to a single source. – Charlie Parker Mar 05 '21 at 14:14
  • @CharlieParker Actually you can. See https://stackoverflow.com/questions/12820616/redirect-nohups-stderr-to-nohup-out – TrueY Mar 08 '21 at 15:14
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

kodmanyagha
  • 932
  • 12
  • 20