Can't execute PHP script using PHP exec
Asked Answered
O

6

16

I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello world example. The calling script looks like:

exec('/usr/bin/php /home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &');

When I run this, the output execoutput.txt contains a copy of the invoking script page, not hello world as I expected.

Why can't I get this PHP script to execute using exec? Note that when I change the command to something like ls -l, the output is a directory listing as expected. btw, in case it matters, I did chmod the called script to 755...

Update - I moved the exec call to the end of the calling script and at least now I don't see the calling script executed in the output. Thx to posters and I will try some of these ideas.

Help!

Thanks Steve

Outtalk answered 19/2, 2009 at 17:9 Comment(0)
C
27

I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:

exec("php-cli  somescript.php");

This worked for me.

Captive answered 19/5, 2010 at 17:49 Comment(6)
This worked for me as well. Wish I saw this answer sooner, would have solved a 5 hour headache.Tic
I've been looking for this for HOURS and was just about to give up. Note that if you're using PHPSuEXEC, this doesn't seem to be necessary, but as soon as you disable suexec, you must use php-cli as specified.Bop
I was banging my head on this for a long time as well. The real puzzle was that using exec with "php" seems to work fine under ubuntu, however when I deployed to a centos server, it does not work. php-cli was the answer, so thanks!Johnnyjumpup
Solved a three-day headache. Our test server did not have this problem, so it has been a major issue trying to fix it.Frasco
Its really great !.. Thanks a lot. if i run exec("php somescript.php"); it takes too much memory, why?Coagulate
You have saved my day as well. For servers where there is no php-cli command, simply use a PHP binary that is not used by the calling script (e.g. /usr/bin/php vs /usr/local/bin/php)Ravishing
D
16

What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.

if you just want the script's output, try:

exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')

however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:

exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')

the above should only output the "Hello World" to the execoutput.

Edit:

Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:

alias pexec='php /home/quote2bi/tmp/helloworld.php'

then

exec('pexec > /tmp/execoutput.txt 2>&1 &')

it seems to be a problem with the way exec handles input as opposed to the shell itself.

-John

Dwayne answered 19/2, 2009 at 17:18 Comment(2)
John - thanks for your reply, but unfortunately, it didn't work for me. When I tried: exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &') ... I got the same results (current page in output file). The second exec blocked (missing the &) - when added I got the same results.Outtalk
Is there any option if exec() function is disabled on my serverMarcos
U
8

The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.

I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:

$script = $argv[1]
$params = implode(' ', array_slice($argv, 2));
$cmd    = "{$script} {$params} > /dev/null &";

$output = array();
$return = 0;
exec("php {$cmd}", $output, $return);

exit((int)$return);

And then you call it using:

exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')

It´s the only way I managed to get this working.

Universe answered 19/2, 2009 at 20:59 Comment(0)
H
0

To avoid the stated problems of PHP in this area, why not put this in inside a shell script? PHP can then execute the shell script which has all the redirections handled internally.

If you need to dynamically change things, then why not write the shell script and then execute it (and of course, clean up afterwards)?

Hyperventilation answered 26/2, 2009 at 2:4 Comment(0)
Y
0

if you are just simply running a php script one possible way to execute the entire code is to use the include() that will run the php file and output any results. You cannot direct the output to a text file but it should appear in the browser window if you're Hello World php script looks like

<?php echo "Hello World!"; ?>

then it will spit that out in the browser. So your second code would look like

<?php include("helloWorld.php");  echo " PHP ROCKS";?>

resulting in a page that would look like,

Hello world! PHP ROCKS

Ylem answered 2/10, 2010 at 18:12 Comment(0)
M
0

This runs as if you run the script from browser.

This came across while working on a project on linux platform.

exec('wget http://<url to the php script>)

Hope this helps!!

Motivate answered 7/1, 2014 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.