views:

193

answers:

4

So I am trying to execute some script from my php code. That lives in page blah.php

<?php
     // ....
     // just basic web site that allows upload of file...
?>

Inside I use system call

if (system("blah.pl arg1") != 0)
{
   print "error\n";
}
else
{
   print "working on it..you will receive e-mail at completion\n";
}

Works great but it waits until it completes until it prints working on it. I am aware that I am calling perl script from php not a typo.

How can I just start program execution and let it complete in background. The blah.pl script handles e-mail notification.

Anyone?

Thanks I appreciate it

A: 

You could probably use the popen function

http://www.php.net/manual/en/function.popen.php

John Boker
+2  A: 

Can you add an ampersand to spawn the process in the background?

if (system("blah.pl arg1 &") != 0)
Tom
+4  A: 

From system function documentation:

Note: 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. Failing to do so will cause PHP to hang until the execution of the program ends.

Simply redirect the output of your Perl script and PHP should be able to run without having to wait for the script to finish.

system("blah.pl arg1 > /dev/null 2>&1 &");
Marek Karbarz
A: 

Since all the PHP command line functions wait for a result - I recommend that you have a separate page to call the emailer script.

In other words, send a request to the email.php page (or whatever) using cURL or the file wrappers which allows you to close the connection before receiving a result. So the process will look something like this.

     page_running.php
     |
    cURL call to (email.php?arg1=0)
     |   |
final output     email.php
        |
        calls system("blah.pl arg1")
Xeoncross
That would work, but has no advantage over redirecting the output of the original call, and would use more resources.
Scott Reynen