2010-03-12 185 views
2

它实际上是PHP和bash的组合:需要帮助理解bash命令

exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, $outputfile, $pidfile)); 

我不明白是什么2>&1 & echo $!是那里?

回答

7

2>&1redirects stderr to stdout,并$!"Expands to the process ID of the most recently executed background (asynchronous) command".

所以,这里是发生了什么:

  1. 你俩标准错误和标准输出$cmd发送到一个名为$outputfile文件。如果您没有执行2>&1,则无法读取文件中的stderr输出。
  2. 以下&表示进程runs in the background
  3. 然后您将$cmd的PID(通过$!获得)附加到$pidfile的末尾。
+0

所以你的意思是它实际上在一行内运行两个命令? – user198729 2010-03-12 12:28:32

+0

是的,它包含:'$ cmd'和'echo' – mouviciel 2010-03-12 12:39:44