In Linux, ">/dev/null 2> & 1" is often encountered in both crontab and Common commands ".
For example, in a crontab job, if you do not want to send an email, you can use either of the following methods:
- First, set mailto = "" to null.
[[email protected] etc]# cat crontabSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=""HOME=/# run-parts01 * * * * root run-parts /etc/cron.hourly02 4 * * * root run-parts /etc/cron.daily22 4 * * 0 root run-parts /etc/cron.weekly42 4 1 * * root run-parts /etc/cron.monthly
- The second is to disable the output result of a command and redirect it to/dev/null.
1 1 ***/sbin/ping-C 5 www.sohu.com>/dev/null 2> & 1
1 2 ***/sbin/ping-C 5 www.sohu.com>/dev/null
The first crontab won't be sent by email even if Ping fails,
In the second crontab, if Ping fails, a large number of error messages will be generated in the/var/spool/mail directory or the/var/spool/clientmqueue directory.
So how to understand ">/dev/null 2> & 1" can be understood separately:
>: Redirection, such as Echo "hello">/root/1.txt
1> stdout: indicates the standard output. The default value is 1.
2> stderr: indicates a standard error.
&: Equivalent to 2> & 1, indicating standard error redirection, equivalent to standard output
/Dev/null: indicates an empty setting file, which is similar to the garbage bin in the win system.
Therefore, ">/dev/null 2> & 1" can also be written as "1>/dev/null 2> & 1"
The statement execution process in the title of this article is as follows:
1>/dev/null: indicates that the standard output is redirected to an empty device file, that is, no information is output to the terminal. In other words, no information is displayed.
2> & 1: Next, the standard error output is redirected to the standard output. Because the standard output has been redirected to the empty device file, the standard error output is also redirected to the empty device file.
For example:
Ping A. B. C #Error MessagePing: Unknown host a. B. C
Ping A. B. C>/dev/null #The error message is the same as above, because only standard output is discarded.
Ping A. B. C>/dev/null 2> & 1 #In this case, the screen will not display the error message, which actually discards the standard output and error output.
- Command> file 2> File
- Command> file 2> & 1
Similarities: the above two commands are implemented to redirect the standard and error output after the command is executed to the file.
Differences: I/O efficiency differences
Command 1> file 2> File
In this way, both stdout and stderr are directly sent to the file, and the file will be opened twice, so that stdout and stderr will overwrite each other, in this way, both fd1 and fd2 are used to seize the file pipeline at the same time.
Command 1> file 2> & 1: This command directly sends stdout to file. stderr inherits the fd1 pipeline and then sends it to file. At this time, the file is opened only once, only one pipeline fd1 is used, which includes stdout and stderr content. In terms of I/O efficiency, the efficiency of the previous command is lower than that of the subsequent command. Therefore, when writing shell scripts, at most, we write command 1> file 2> & 1, or "command> file 2 & 1"