其实是php和bash的结合:
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile));
我不明白 2>&1 & echo $!
有什么用?
最佳答案
2>&1
redirects stderr to stdout , 和 $!
"Expands to the process ID of the most recently executed background (asynchronous) command".
所以,这是发生了什么:
- 您将
$cmd
的 stderr 和 stdout 都发送到名为$outputfile
的文件。如果您没有执行2>&1
,您将无法读取文件中的 stderr 输出。 - 后面的
&
表示进程runs in the background . - 然后将
$cmd
的 PID(通过$!
获得)附加到$pidfile
的末尾。
关于php - 需要帮助理解 bash 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2432578/