1

我需要知道,如果我通过 exec 命令调用 php 脚本执行命令并且由于任何原因导致脚本执行失败说“找不到文件”,那么我该如何找到它。我有以下命令:

    $cmd="php testfile.php" ;
    $outputfile="testoutput.txt";
    exec(sprintf("%s > %s 2>&1 & echo $!", $cmd, $outputfile),$pidArr, $status);

如果出现错误,exec 命令返回 -1,但在这种情况下,exec 正在成功执行,即 $status 在我的情况下为 0,但“php testfile.php”命令失败,输出进入 testoutput.txt。但是我需要知道方法,以便如果命令失败,我可以在 exec 之后识别它。我可以考虑读取 testoutput.txt 和 grep 以查找失败或错误字的选项,但我认为它不可靠。

提前致谢!

4

2 回答 2

2

您可以通过传递可选的第二个参数来接收 exec 函数的输出结果:

因此,您可以使用该 3rd arg 执行 exec(),然后检查它是否为非零的错误条件。

exec("blah blah blah", $output, $result); 
if($result > 0) 
{ 
   die('error message here'); 
}  

如果通过第二个参数没有找到错误,可以搜索apache的错误日志,例如在Ubuntu Server 12.10中通过命令$ tail /var/log/apache2/error.log

让我知道我是否可以为您提供更多帮助。

于 2013-07-02T07:08:54.337 回答
1

http://php.net/manual/en/function.exec.php

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

$status=0 如果没有错误,> 0 如果错误

于 2013-07-02T07:05:18.833 回答