在php中通过exec/system执行linux命令时获取退出代码


getting linux command exit code when executed via exec/system in php

我的术语可能不合适,但这里是:

让我们假设一个执行:

/bin/somecommand

在php中使用exec或system

上面的命令返回"退出代码"(这是一个可能关闭的术语)"1"。

可以通过php获取这个值吗?

如果可能的话,可以在不使用"parent"bash脚本的情况下执行此操作。我们希望能够直接从php获取它,而不必运行父bash脚本,并让该脚本回显退出代码。

谢谢!

exec()的手册显示,您可以提供可选的第三个参数来收集返回状态(退出代码)。类似地,对于system(),第二个可选参数。

该页面的示例:

<?php
echo '<pre>';
// Outputs all the result of shellcommand "ls", and returns
// the last output line into $last_line. Stores the return value
// of the shell command in $retval.
$last_line = system('ls', $retval);
// Printing additional info
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
?>