3

What is the correct order between nohup, & and redirection, when using together?

Why are other orders not correct?

For example,

$ nohup firefox& &> /dev/null

$ nohup firefox &> /dev/null  &

and I don't know how to change the order between nohup with redirection and &.

Another example,

$ nohup sleep 1000 & &>/dev/null

doesn't redirect to /dev/null, but still to ./nohup.out.

2

I would start with section SHELL GRAMMAR from manual page for bash:

Simple Commands

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator.

Therefore redirection (&>/dev/null) goes first and the control operator follows (&).

Edit: And to make the answer complete, the nohup is the first part of the simple command. It does not have any special meaning in bash.

Exactly your example (except the /dev/null which only makes it complicated):

jakuje@E6430:/tmp$ rm nohup.out out
jakuje@E6430:/tmp$ nohup sleep 1 & &>out
[1] 10876
nohup: ignoring input and appending output to ‘nohup.out’
[1]+  Done                    nohup sleep 1
jakuje@E6430:/tmp$ cat nohup.out             # empty
jakuje@E6430:/tmp$ cat out                   # empty
jakuje@E6430:/tmp$ rm nohup.out
jakuje@E6430:/tmp$ nohup sleep 1 &>out &
[1] 10882
[1]+  Done                    nohup sleep 1 &> out
jakuje@E6430:/tmp$ cat nohup.out             # no file
cat: nohup.out: No such file or directory
jakuje@E6430:/tmp$ cat out                   # output of nohup
nohup: ignoring input
  • do you mean only nohup firefox &> /dev/null & is correct? – Tim Mar 1 '16 at 18:13
  • yes. As you write. – Jakuje Mar 1 '16 at 18:14
  • linuxg.net/… says nohup firefox& &> /dev/null. So the webpage isn't correct? – Tim Mar 1 '16 at 18:17
  • You can't believe everything what is written on the Internet. It redirects to /dev/null so it does not matter much. Sorry, but giving it a try would not hurt, would it? See my example. The first one redirects the output of immediately backgrounded process, which is empty (but you get the output in nohup.out). The second one redirects the actual output of the command, in which you are interested. – Jakuje Mar 1 '16 at 18:28
  • Didn't I mention I have tried in my post? – Tim Mar 1 '16 at 18:47
1

The nohup command is the command you are actually running in this case. The word firefox is not interpreted by the shell as a command; it is simply passed as an argument to nohup. What the nohup command does with that argument is "none of the shell's business".

Of course what nohup does is to take that argument and runs it as a command.

The point is that what you're actually doing from the shell's point of view is redirecting the output of nohup, not the output of firefox.

However, nohup will do different things with the output of the command that it runs, depending on where its own output is pointed. From the POSIX specs for nohup (emphasis added):

If the standard output is a terminal, all output written by the named utility to its standard output shall be appended to the end of the file nohup.out in the current directory. If nohup.out cannot be created or opened for appending, the output shall be appended to the end of the file nohup.out in the directory specified by the HOME environment variable. ...

If standard error is a terminal and standard output is open but is not a terminal, all output written by the named utility to its standard error shall be redirected to the same open file description as the standard output. ...

So due to some inner magic of nohup, your redirections work as expected if you run:

nohup utility >/dev/null &

Note that you don't need to redirect both stderr and stdout, as the nohup command will redirect the stderr of utility depending on where nohup's own output is pointing to. (Not to mention that &> isn't portable.)

There is also a portability issue with your input. Since your stdin is connected to your terminal, it is implementation-specific what will happen with it. (See note under "Rationale" in the nohup POSIX specs.) Instead you might be better off using:

nohup firefox >/dev/null </dev/null &

And as already mentioned in comments, there is no circumstance with nohup or any other command where you should ever put a redirection after the backgrounding &.

  • Thanks. From my experiement, both stdout and stderr for nohup need to be redirected, so that there will be no nohup.out file created. – Tim Apr 18 '18 at 19:04
  • @Tim, what distribution are you using and what version of nohup? – Wildcard Apr 18 '18 at 20:01
  • ubuntu 16.04 and nohup (GNU coreutils) 8.25 – Tim Apr 18 '18 at 20:15

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.