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

ano*_*one 1 php linux shell system exec

我的术语是关闭的,但这里有:

假设一个人执行:

/斌/ somecommand

在php中使用exec或system

上面的命令返回'退出代码'(这是可能关闭的术语)'1'.

是否有可能通过PHP获取此值?

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

谢谢!

Oli*_*rth 7

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;
?>
Run Code Online (Sandbox Code Playgroud)