无法从php代码运行shell脚本


Unable to run shell script from php code

我在/var/www/中有一个shell脚本(即mat.sh),在同一目录中也有一个php脚本(即n1.php)。当我从bash命令运行shell脚本时,它正在运行,但当我从apache2php服务器执行相同的脚本时,却没有执行。

mat.sh包含。。

#!/bin/bash
cat <<EOF | /var/www/matlab  -nodesktop -nosplash -nodisplay /> result.out
a=add(2,3);
disp('this is done');
disp(a);
exit
EOF

注意:/var/www/matlab是matlab链接的目录

n1.php包含。。

<html>
<body>
<?php
if ($_GET['run'])
{
  # This code will run if ?run=true is set.
 echo "Execution starts here...<br/>'n";
 echo exec("whoami");
 echo "<br/>'n";
exec ("chmod a+x mat.sh", $output, $return);
if ($return != 0)
{
        //An error occured, fallback or whatever
        echo "Execution failed<br/>'n";
}
exec ("sh mat.sh");
}
?>
<!-- This link will add ?run=true to your URL, myfilename.php?run=true -->
<a href="?run=true">Click Me!</a>

请帮帮我……

完成chmod后是否收到错误消息?

确保mat.sh与您的apache2服务器实例具有相同的所有者,否则这行

exec ("chmod a+x mat.sh", $output, $return);

将失败,因为只有所有者才能更改权限。