NOHUP and Redirection of standard input/output/error
Examples
JMX_PORT=1234 nohup command </dev/null >logs/nohup.out 2>&1 & # Sets a variable only for command, redirects standard input from null (disables input), then redirects standard output to logs/nohup.out & finally redirects standard error to standard output JMX_PORT=1234; nohup command </dev/null >logs/nohup.out 2>&1 & # Sets the variable for current shell, redirects standard input from null (disables input), then redirects standard output to logs/nohup.out & finally redirects standard error to standard output nohup command >/dev/null 2>&1 & # First redirects standard output to null & then redirects standard error to standard output nohup command &> /dev/null & # Same as above, but only in BASH nohup command </dev/null >/dev/null 2>&1 & # Redirects standard input from null (disables input), then redirects standard output to null & finally redirects standard error to standard output nohup command </dev/null >logs/nohup.out 2>&1 & # Redirects standard input from null (disables input), then redirects standard output to logs/nohup.out & finally redirects standard error to standard output nohup command 2>&1 >/dev/null & # Redirects standard error to current standard output (which is terminal till now), and then redirects standard output to null. This is wrong, as it will still log error messages to terminal.
NOHUP
nohup is a POSIX command to ignore the HUP (hangup) signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout.nohup command >/dev/null 2>&1 # doesn't create nohup.outOutput normally goes to nohup.out instead of terminal, unless redirected. If you redirect the output of the command to somewhere else, including /dev/null - that's where it goes instead.
If you're using nohup, that probably means you want to run the command in the background by putting another & on the end of the whole thing:
nohup command >/dev/null 2>&1 & # runs in background, still doesn't create nohup.out
On Linux, running a job with nohup automatically closes its input as well. On other systems, notably BSD and OS X, that is not the case, so when running in the background, you might want to close its input manually. While closing input has no effect on the creation or not of nohup.out, it avoids another problem: if a background process tries to read anything from standard input, it will pause, waiting for you to bring it back to the foreground and type something. So the extra-safe version looks like this:
nohup command </dev/null >/dev/null 2>&1 & # completely detached from terminal
Note, however, that this does not prevent the command from accessing the terminal directly, nor does it remove it from your shell's process group. If you want to do the latter, you can do so by running disown with no argument as the next command, at which point the process is no longer associated with a shell "job" and will not have any signals (not just HUP) forwarded to it from the shell.
Redirecting file descriptors
In *nix systems, every source of input or target of output has a number associated with it called a "file descriptor", or "fd" for short. Every running program ("process") has its own set of these, and when a new process starts up it has three of them already open for it to write to:- standard input (fd 0): open for the process to read from
- standard output (fd 1)
- standard error (fd 2)
If you just run a command in a terminal window, then by default, anything you type goes to its standard input, while both its standard output and standard error get sent to that window. But you can ask the shell to change where any or all of those file descriptors point before launching the command; that's what the redirection (<, <<, >, >>) and pipe (|) operators do.
pipe
The pipe is the simplest of these: command1 | command2 arranges for the standard output of command1 to feed directly into the standard input of command2. This is a very handy arrangement that has led to a particular design pattern in UNIX tools (and explains the existence of standard error, which allows a program to send messages to the user even though its output is going into the next program in the pipeline). But you can only pipe standard output to standard input; you can't send any other file descriptors to a pipe without some juggling.Redirection
The redirection operators are friendlier in that they let you specify which file descriptor to redirect. So 0<infile reads standard input from the file named infile, while 2>>logfile appends standard error to the end of the file named logfile. If you don't specify a number, then input redirection defaults to fd 0 (< is the same as 0<), while output redirection defaults to fd 1 (> is the same as 1>).Also, you can combine file descriptors together: 2>&1 means "send standard error wherever standard output is going". That means that you get a single stream of output that includes both standard out and standard error intermixed with no way to separate them anymore, but it also means that you can include standard error in a pipe.
So the sequence >/dev/null 2>&1 means "send standard output to /dev/null" (which is a special device that just throws away whatever you write to it) "and then send standard error to wherever standard output is going" (which we just made sure was /dev/null). Basically, "throw away whatever this command writes to either file descriptor".
When nohup detects that neither its standard error nor output is attached to a terminal, it doesn't bother to create nohup.out, but assumes that the output is already redirected where the user wants it to go.
The /dev/null device works for input, too; if you run a command with /dev/null <&1, but that winds up creating a process with an input file descriptor open on an output stream, so instead of just hitting end-of-file, any read attempt will trigger a fatal "invalid file descriptor" error.
Comments