[英]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.