Detailed usage of nohup and dev and null 2greater than1 meaning

  • 2021-06-28 14:38:41
  • OfStack

nohup command: If you are running a process and you do not think it will end when you exit the account, you can use the nohup command.This command allows you to continue running the process after you exit the account/close the terminal.nohup means no suspension (n ohang up).

The general form of this command is: nohup command &


ls xxx 1>out.txt 2>&1
nohup /mnt/Nand3/H2000G >/dev/null 2>&1 &

For & 1 More precisely, it should be file descriptor 1, and 11 generally represents STDOUT_FILENO, which is actually an dup2 (2) call. His standard output to all_result, then copy the standard output to file descriptor 2 (STDERR_FILENO), with the consequence that file descriptors 1 and 2 point to the same file table item or that the incorrect output is merged.0 means keyboard input 1 means screen output 2 means error output.Redirect the standard error to standard output and throw it under/DEV/NULL.It is a common saying to throw all standard outputs and standard errors into the dustbin.


command >out.file 2>&1 &

command > out.file redirects the output of command to the out.file file, that is, the output is not printed to the screen, but to the out.file file.2 > & 1 is to redirect standard errors to standard output, where standard output has been redirected to the out.file file, that is, to the out.file file.Last 1 & Is to have the command executed in the background.

Imagine 2 > What does 1 mean, 2 and > Combination represents error redirection, while 1 represents error redirection to a file 1, not to standard output;
Replace with 2 > & 1, & When combined with 1, it represents standard output and becomes an error redirecting to standard output.

You can use
ls 2 > 1 Under test 1, no error with 2 files will be reported, but an empty file 1 will be output.
ls xxx 2 > 1 test, no error output from xxx file to 1;
ls xxx 2 > & 1 test, this file will not be generated, but the error runs to standard output;
ls xxx > out.txt 2 > & 1, actually ls xxx 1 > out.txt 2 > & 1;Redirection Symbol > By default, 1, errors and outputs are passed to out.txt.

Why 2 > & 1 To write after?


command > file 2>&1 

First is command > file redirects standard output to file, 2 > & 1 is the behavior of standard errors that copy the standard output, that is, they are also redirected to file. The end result is that both standard output and errors are redirected to file.


command 2>&1 >file 

2 > & 1 The standard error copies the behavior of the standard output, but at this point the standard output is still in the terminal. > The output is redirected to file after file, but the standard error remains at the terminal.

With strace you can see:

1. command > file 2 > & 1
The key sequence of system calls for redirection in this command is:


open(file) == 3 
dup2(3,1) 
dup2(1,2) 

2. command 2 > & 1 > file
The key sequence of system calls for redirection in this command is:


dup2(1,2) 
open(file) == 3 
dup2(3,1) 

Why use/dev/null 2 > & 1. This command means redirecting all standard and error outputs to / dev / null, which means discarding all generated information. Here's what I mean for you, command > file 2 > file and command > file 2 > & 1 What's different.

First~command > file 2 > file means to send the standard output information generated by the command and the wrong output information to file.command > file 2 > In the case of file, both stdout and stderr are sent directly to file. file is opened twice so that stdout and stderr cover each other. This is equivalent to using FD1 and FD2 pipes to preempt file at the same time.

command > file 2 > & 1 This command sends stdout directly to file, which inherits the FD1 pipeline and is then sent to file. At this time, file has only been opened once, and only one pipeline has been used, FD1, which contains the contents of stdout and stderr.

From IO efficiency, the first command is less efficient than the latter, so we use command more often when writing shell scripts > file 2 > & 1 This way of writing.


Related articles: