繁体   English   中英

无法从PHP代码运行Shell脚本

[英]Unable to run shell script from php code

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

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-server-instance具有相同的所有者,否则此行

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

将失败,因为只有所有者可以更改权限。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM