[英]exec and shell_exec not working in php
我在使用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中工作正常。 和诸如whoami
和ls
类的简单命令也可以正常工作。 我觉得我在想什么
编辑:尝试了什么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");
我得到相同的输出;
尝试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;
?>
尝试将您的Web服务器用户添加到对批处理目录具有rw权限的组。 作为root用户或使用sudo:
usermod -G groupname username
然后在批处理目录上:
chmod -R g+w batch
显然我使用的是错误的php文件/路径> <像一个虚拟的,感谢您的帮助
尽管这是一篇旧文章,但这是我对其他用户的回答。
例
$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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.