When configuring scheduled tasks on crontab, the program output is often redirected to a file or /dev/null. Such as 15 * * * * * / PHP/data0 log2db/logdbplan. PHP > / TMP/logdbplan log 2 > &1 writes the application log and error logdbplan. The log file, >/ TMP /logdbplan.log is a short form of 1>/ TMP /logdbplan.log
0, 1, and 2 Indicates the description of the file descriptor
By default, the system opens three file descriptors for the program, which are 0,1 and 2 respectively. Of course, after the program starts, you can choose to close some file descriptors. You can use lsof -a -p process PID-d0,1,2 to check the specific information
0 and stdin: standard input 1 and stdout: standard output 2 and stderr: standard error
>, >> Redirect
> is to clear the file and then write. >> equivalent to append mode. We can simulate this in code
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]){ close(1); // Close (2); / / close the standard error / / UXIN from 0 to start looking for can use the file descriptor, returns 1 first open the file descriptor open (". / the out. TXT ", O_CREAT | O_WRONLY | O_TRUNC) will be passed. open("./err.txt", O_CREAT | O_WRONLY | O_TRUNC ); Printf (" standard output --1\n"); Fprintf (stdout, "stdout --2\n"); Write (1, "stdout --3\n", strlen(" stDout --3\n")); Fprintf (stderr, "standard error --1\n"); Write (2, "serror --2\n", strlen(" serror --2\n")); return 0; }Copy the code
In the code we turn off the standard output and standard error descriptors, and then turn on the STdout and stderr descriptors. The program is compiled and executed as follows:You can see that three logs from standard output are written to out.txt and two error messages are written to err.txt, similar to cmd 1>out.txt 2>err.txt
Execute the program, however2 > &1
And how does that happen? Let’s modify the program and try assigning the standard output to the standard error descriptor as follows:
#include <stdio.h> #include <fcntl.h> #include <unistd.h> #include <string.h> int main(int argc, char *argv[]){ close(1); // Close (2); / / close the standard error / / UXIN from 0 to start looking for can use the file descriptor, returns 1 first open the file descriptor int fd = open (". / the out. TXT ", O_CREAT | O_WRONLY | O_TRUNC) will be passed. // open("./err.txt", O_CREAT | O_WRONLY | O_TRUNC ); Printf (" standard output --1\n"); Fprintf (stdout, "stdout --2\n"); Write (1, "stdout --3\n", strlen(" stDout --3\n")); // Assign stderr = stdout to stderr; Fprintf (stderr, "standard error --1\n"); Write (fd, "serror --2\n", strlen(" serror --2\n")); return 0; }Copy the code
As in the above code, we simply assign the value to stderr and compile and execute it as follows:You can see that 2 standard errors and 3 standard outputs were successfully written to out.txt.>/tmp/logdbplan.log 2>&1
The logdbplain.log file handle is given to the standard output, while2 > &1
the&
Standard error refers to the file descriptor of standard output, so standard output and standard error messages are written to out.txt.
Contents: File IO
Here’s a quick explanation of why the result in the two screenshots is not the order in which the code is written, it’s all about system optimization. Printf and fprintf copy data into the cache of the standard library (library C in this case) and return it, operating on the cache of user process space. At some point the call to write is copied to the kernel cache and eventually written to disk. Printf and fprintf combine multiple user writes to reduce disk write times and improve performance.
Write asynchronously copies data to the kernel’s Page cache, switching between kernel and user mode. The kernel does not write the data to disk directly after it gets it. It is determined by the IO scheduling system. Of course, you can also set write synchronous write to disk.
The IO scheduling system also merges multiple writes and sorts write events according to the direction of disk selection and the direction of the magnetic head. Multiple random writes are optimized to be written in sequence, because the magnetic head rotation addressing is very slow, while the read and write are relatively fast. If SSD disks are used, you do not need to sort them. You only need to merge write events to reduce disk write times