【问题标题】:exec and shell_exec not working in phpexec 和 shell_exec 在 php 中不起作用
【发布时间】:2013-03-24 06:43:37
【问题描述】:

我遇到了 exec 和 shell_exec 的问题,我查看了这里的每个帖子,但似乎无法找到问题所在。我正在使用的代码:

chdir('/');
$handle = shell_exec("/usr/bin/php /home/donor/public_html/batch/testlaunch.php 2>&1");
echo $handle .":". gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;

屏幕如下所示:

...
X-Powered-By: PHP/5.3.21 Content-type: text/html (repeated about 100 times)
X-Powered-By: PHP/5.3.21 Content-type: text/html 
PHP Warning: shell_exec() [function.shell-exec]: Unable to execute '/usr/bin/php /home/donor/public_html/batch/testlaunch.php 2>&1' in /home/donor/public_html/batch/test.php on line 4 X-Powered-By: PHP/5.3.21 Content-type: text/html 
Warning: shell_exec() [function.shell-exec]: Unable to execute '/usr/bin/php /home/donor/public_html/batch/testlaunch.php 2>&1' in /home/donor/public_html/batch/test.php on line 4
:boolean PHP Warning: fread() expects parameter 1 to be resource, boolean given in /home/donor/public_html/batch/test.php on line 6 
Warning: fread() expects parameter 1 to be resource, boolean given in /home/donor/public_html/batch/test.php on line 6
:string PHP Warning: fread() expects parameter 1 to be resource, string given in /home/donor/public_html/batch/test.php on line 6 (repeated another 100ish times)

PHP 安全模式已关闭。文件所在文件夹的权限 .../batch/ 和 /usr/bin/php 都设置为 755。除 php 外,所有文件的用户都相同(php 文件用户为 root)。 php.ini 文件中没有禁用任何内容。在 CLI 中运行良好。 whoamils 等简单的命令可以正常工作。我觉得我错过了什么

编辑: 尝试了cpattersonv1所说的并得到了:

X-Powered-By: PHP/5.3.21 Content-type: text/html sh: fork: retry: Resource temporarily unavailable sh: fork: retry: Resource temporarily unavailable sh: fork: retry: Resource temporarily unavailable sh: fork: retry: Resource temporarily unavailable X-Powered-By: PHP/5.3.21 Content-type: text/html sh: fork: retry: Resource temporarily unavailable sh: fork: retry: Resource temporarily unavailable sh: fork: retry: Resource temporarily unavailable sh: fork: retry: Resource temporarily unavailable sh: fork: Resource temporarily unavailable X-Powered-By: PHP/5.3.21 Content-type: text/html 2540000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

编辑 2:有谁知道为什么它看起来像是多次尝试命令?

编辑 3:我将命令放在 .sh 文件中并尝试运行它。在 CLI 中工作,但在 php 中也不能工作

chdir('/');
$handle = exec(". /home/donor/public_html/batch/test.sh");

我得到相同的输出;

【问题讨论】:

  • var_dump($handle) 会得到什么
  • "X-Powered-By: PHP/5.3.21 Content-type: text/html string(641) "X-Powered-By: PHP/5.3.21 Content-type: text/html字符串(573)

标签: php shell permissions exec shell-exec


【解决方案1】:

虽然这是一篇旧帖子,但这是我对其他用户的回答。

  1. 将目录更改为可执行路径
  2. 在没有可执行文件路径的情况下调用 shell_exec()
  3. 将目录恢复为原始目录(以免妨碍其他代码部分)。

例子

$current_dir = getcwd();
$executable_dir = "\executables\";
chdir($executable_dir);

$output1 = shell_exec("executable.exe file_to_run.ext $parameter_to_pass");
var_dump($output1);

chdir($current_dir);

【讨论】:

  • 给那些“其他用户”的注意事项:注意第二行末尾的\":这是双引号的转义,会给你一个错误。替换` with /`(在Linux上),或者更好地使用常量DIRECTORY_SEPARATOR,这样:DIRECTORY_SEPARATOR . "executables" . DIRECTORY_SEPARATOR
【解决方案2】:

显然我使用了错误的 php 文件/路径 >

【讨论】:

    【解决方案3】:

    尝试 passthru intead,这样如果需要一段时间,执行就不会受到任何限制:

    <?php
    
     passthru("/usr/bin/php /home/donor/public_html/batch/testlaunch.php 2>&1",$pass_result);
     echo $pass_result; 
    
    ?>
    

    或者这个:

    <?php
    
     passthru("/usr/bin/php /home/donor/public_html/batch/testlaunch.php",$pass_result,$error);
     echo $pass_result; 
     echo $error; 
    
    ?>
    

    【讨论】:

      【解决方案4】:

      尝试将您的网络服务器用户添加到对批处理目录具有 rw 权限的组。以 root 身份或使用 sudo:

      usermod -G groupname username
      

      然后在批处理目录上:

      chmod -R g+w batch
      

      【讨论】:

      • 你不需要使用 chmod 因为你的权限已经是 755
      • 如果我给它写权限,当我访问页面并且批处理文件夹已经是网络用户的捐赠者时,它会给我一个内部服务器错误
      • 再次阅读您的帖子,我注意到您的 shell_exec 指向 /usr/bin/php 并且您在描述中引用了 /bin/php。使用 'which php' 知道你服务器上 php 的位置
      • 对不起,我只是缩短它应该是 /usr/bin/php 这就是所有 php 进程在服务器“进程管理器”上所说的
      • 是否还有其他目录我也需要授予权限?
      猜你喜欢
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 2013-08-07
      相关资源
      最近更新 更多