public static function resetStd() { if (!static::$daemonize || static::$_OS !== 'linux') { return; } global $STDOUT, $STDERR; $handle = fopen(static::$stdoutFile, "a"); if ($handle) { unset($handle); //关闭标准输出和标准错误 @fclose(STDOUT); @fclose(STDERR); //a 写入方式打开,将文件指针指向文件末尾。若是文件不存在则尝试建立之。 //把标准输出和错误 定位到 /dev/null $STDOUT = fopen(static::$stdoutFile, "a"); $STDERR = fopen(static::$stdoutFile, "a"); } else { throw new Exception('can not open stdoutFile ' . static::$stdoutFile); } }
命令行下运行以上程序,将不会在控制台输出任何内容,输出内容将被重定向到/dev/null中,很是诧异,一直不理解。$STDOUT, $STDERR 这并非内置的变量, 只是普通的一个变量名称而已。为何经过这样处理,就能实现输出重定向呢?php
因而只能google出stackoverflow答案 : https://stackoverflow.com/questions/6472102/redirecting-i-o-in-phplinux
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 will 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 when making a daemon script in PHP.google
解释说:spa
若是你关闭了标准输出,标准错误输出文件描述符,那么你打开的前三个文件描述符将成为新的标准输入、输出、错误的描述符。命令行
使用$STDIN, $STDOUT纯粹是障眼法而已, 必须指定为全局变量,不然文件描述符将在函数执行完毕以后被释放。指针
利用这个特性,后台脚本运行时,就能够直接输出并被记录到相关的文件中,给咱们跟踪程序带来极大的方便。code
意思是把标准错误流 (stdout) 、标准错误流(stderr)重定向到设备/dev/null上。blog
/dev/null 是类Unix系统中的一个特殊文件设备,他的做用是接受一切输入它的数据并丢弃这些数据。一般被当作垃圾桶来用。three
将输出流重定向到它上面,就是丢弃这个输出流上的全部输出。
尝试从/dev/null读取数据,会马上获得一个EOF。
顺便,类Unix系统中,0表明标准输入流(stdin),1表明标准输出流(stdout),2表明标准错误流(stderr)。