Can often be seen in scheduled tasks. For example, our company's planned task example:
*/2 * * * * root cd /opt/xxxx/test_S1/html/xxxx/admin; php index.php task testOne >/dev/null 2>&1*/2 * * * * root cd /opt/xxxx/test_S1/html/xxxx/admin; php index.php task testTwo >/dev/null 2>&1
For & 1, more accurately, it should be file descriptor 1, and 1 identifies the standard output, stdout.
For 2, it means standard error, stderr.
2>&1 means redirecting standard error to standard output. Here standard output has been redirected to /dev/null. Then the standard error will also be output to /dev/null
You can think of /dev/null as a "black hole". It is equivalent to a write-only file. All content written to it will be lost forever. And If you try to read content from it, you will get nothing.
Occasionally, you can add & at the end of the command to let the program execute in the background.
Why should 2>&1 be written at the end?
index.php task testOne >/dev/null 2>&1
We can understand that the left side is the standard output, okay, now the standard output is directly input into /dev/null, and 2>& 1 redirects standard error to standard output, so when the program generates an error, it is equivalent to the error flowing to the left, and the left side is still input to /dev/null.
can be understood as, if written in the middle, it will separate the standard output specified output file
You can use
ls 2>1 Test it, it will not report the error that there is no 2 file, but it will output an empty file 1;
ls xxx 2>1 test, the error that there is no xxx file is output to 1;
ls xxx 2>&1 Test, the file 1 will not be generated, but the error will go to the standard output;
ls xxx >out.txt 2>&1, it can actually be replaced by ls xxx 1>out.txt 2>&1;Redirection symbol>The default is 1, and errors and output are transmitted to out.txt.
The above is the detailed content of The meaning of '2>&1' in linux shell. For more information, please follow other related articles on the PHP Chinese website!