简体   繁体   English

无法从PHP代码运行Shell脚本

[英]Unable to run shell script from php code

I have a shell script(ie mat.sh ) in /var/www/ and also have a php script(ie n1.php ) in the same directory. 我在/var/www/有一个shell脚本(即mat.sh ),并且在同一目录中也有一个php脚本(即n1.php )。 When i am running the shell script from bash command it is running, but when im executing the same from the apache2 php server it is not executing. 当我从bash命令运行shell脚本时,它正在运行,但是当我从apache2 php服务器执行相同脚本时,它没有执行。

mat.sh contains.. 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

note: /var/www/matlab is the directory of matlab link 注意: /var/www/matlab是matlab链接的目录

n1.php contains.. 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>

Please help me..... 请帮我.....

Do you get your error message when having done chmod ? 完成chmod您是否收到错误消息?

Make sure that mat.sh has the same owner as your apache2-server-instance otherwise this line 确保mat.sh与您的apache2-server-instance具有相同的所有者,否则此行

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

will fail, because only the owner can change permissions. 将失败,因为只有所有者可以更改权限。

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

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