简体   繁体   中英

exec and shell_exec not working in php

I am having problems with exec and shell_exec i have looked at every post on here and cant seem to find the problem. the code i am using :

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;

the screen looks like this:

...
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 safemode is turned off. Permissions for the folder the files is in .../batch/ and the /usr/bin/php are both set to 755. The user for all files are the same except for php (php file user is root). Nothing is disabled in the php.ini file. Works fine in the CLI. and simple commands such as whoami and ls work fine. I feel like i am missing something

EDIT: tried what cpattersonv1 said and got:

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

EDIT 2: does anyone know why it looks like it is trying the command more then once?

EDIT 3: I put the command inside a .sh file and tried to run that. works in CLI but again does not work from php

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

I get the same output;

Try passthru intead, that way there aren't any limits on the execution if it takes a while:

<?php

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

?>

or this:

<?php

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

?>

Try adding your webserver user to a group with rw permissions to the batch directory. Either as root or using sudo:

usermod -G groupname username

then on the batch directory:

chmod -R g+w batch

显然我使用的是错误的php文件/路径> <像一个虚拟的,感谢您的帮助

Though this is an old post, here is my answer for other users.

  1. Change the directory to the executable path
  2. Call shell_exec() without the path of the executable
  3. Revert the directory back to the original (so that some other part of code is not hampered).

Example

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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