The code in the Resetstd method in work.php in Workerman is as follows
Public Static functionresetstd () {if(!Static::$daemonize||Static::$_os!== ' Linux ') { return; } Global $STDOUT,$STDERR; $handle=fopen(Static::$stdoutFile, "a"); if($handle) { unset($handle); //turn off standard output and standard errors@fclose(STDOUT); @fclose(STDERR); //A write mode opens, pointing the file pointer to the end of the file. If the file does not exist, try to create it. Locating standard outputs and errors to/dev/null $STDOUT=fopen(Static::$stdoutFile, "a"); $STDERR=fopen(Static::$stdoutFile, "a"); } Else { Throw New Exception(' Can not open stdoutfile '.Static::$stdoutFile); } }
Command line run the above program, will not output any content in the console, the output will be redirected to/dev/null, very surprised, has not understood. $STDOUT, $STDERR This is not a built-in variable, it's just a generic variable name. Why is it possible to achieve output redirection through this process?
So only Google out StackOverflow answer: https://stackoverflow.com/questions/6472102/redirecting-i-o-in-php
fclose (STDIN); fclose (STDOUT); fclose (STDERR); $STDIN fopen ("/tmp/some-named-pipe", "R"); $STDOUT fopen ("/tmp/foo.out", "WB"); $STDERR fopen ("/tmp/foo.err", "WB");
Because when you close the standard input, output and error file descriptors, the first three new descriptors would become The NEW standard input, output and error file descriptors.
In my example here I redirected standard input to/dev/null and the output and error file descriptors to log files. This is common practice if making a daemon script in PHP.
explained that:
If you turn off standard output and the standard error output file descriptor, the first three file descriptors you open will be the new standard input, output, and error descriptors.
With $stdin, $STDOUT is purely a decoy and must be specified as a global variable, otherwise the file descriptor will be freed after the function has finished executing.
With this feature, when the background script runs, it can be directly output and recorded in the relevant files, to our tracking program to bring great convenience.
To understand the Resetstd method through the answers above
This means redirecting the standard error stream (STDOUT), the standard error stream (STDERR) to the device/dev/null.
/dev/null is a special file device in Unix-like systems, and his role is to accept all input data and discard the data. It is usually used as a trash can.
Redirecting the output stream to it is discarding all output on the output stream.
Attempting to read data from/dev/null will immediately get an EOF.
By the way, in Unix-like systems, 0 represents the standard input stream (stdin), 1 represents the standard output stream (stdout), and 2 represents the standard error stream (stderr).
Workerman Source Analysis (Resetstd method, PHP stdin, STDOUT, stderr redirection)