0

Possible Duplicate:
Call another PHP script and return control to user before the other script completes
How to run a PHP script asynchronously from a another PHP script?

I have some problems on using exec() in PHP but I have no idea to solve it. I want to use exec() to execute another php script in same directory 'asynchronously'. I open a.php in browser to call b.php but it doesn't work. (the localhost is xampp on windows 7)

a.php:

<?php
exec('php \b.php', $output, $r);
print_r($output);
print_r($r);
?>

b.php:

#!/usr/bin/php
<?php
echo time();
?>

The output in browser

Array ( ) 1

I am super beginner on using something related PHP CLI... I have no idea about this... Can anyone give some simple examples? For example, what should be written in a.php and b.php.

Thanks a lot if you can give me some guides or advice!


EDIT:

I tried following code and it works but it does not run 'asynchronously'... How can I redirect the output?

Open callexec.php in browser.

callexec.php

<?php
exec('C:\xampp\php\php.exe testexec.php');
?>

testexec.php

<?php
echo "start: ",time(),"\n";
sleep(10);
echo "\n";
echo "end: ",time(),"\n";
?>

Thanks again.

Community
  • 1
  • 1
Timespace
  • 5,101
  • 7
  • 23
  • 32
  • Why is there a backslash before `b.php`? Have you enabled error_reporting? Is `php` really in the PATH? – mario Sep 29 '12 at 16:52
  • If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. I would like to ask how to redirect the output to a file or another output stream? – Timespace Sep 30 '12 at 03:18

3 Answers3

1

As you are already telling your shell-script that it is a php script with #!/usr/bin/php, you don't need to invoke php for your script, you can just call it like any script from the command line:

exec('b.php', $output, $r);

By the way, I am assuming that you can call b.php from the command line without any problems.

jeroen
  • 91,079
  • 21
  • 114
  • 132
0

There might be a problem with port numbers if you have another server on the same platform. For instance it could be an oracle database server, mysql, Apache tomcat, etc. Please confirm whether the port numbers of the different servers are the same.

will-hart
  • 3,742
  • 2
  • 38
  • 48
AKN
  • 1
0

Why make things complicated? Just use include or require?

Ed Heal
  • 59,252
  • 17
  • 87
  • 127