1

I'm working on a web application that needs to send a lot of HTTP requests and update the table, this will block the PHP from executing. So I though I might have to write a separate PHP script and run it via my main application. I tried Exec but still the program waits until the script is executed.

exec('php do_job.php');

I even tried redirecting the output to a file as PHP.Net suggests:

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.

$result = exec('php do_job.php > output.txt &',$output);

But still no success ... Further down the same page I came accross this:

$command = 'php do_job.php';
$shell = new COM("WScript.Shell");
$shell->run($command, 0, false);

Still no sucess ... Lastly I tried:

pclose(popen("start /B ". $command, "r"));

What am I doing wrong here?

I'm developing my app on localhost (XAMPP - Windows), later I'll be releasing it on a Linux host. My last resort would be to run the script via CRON jobs. Is this the only way?

3
  • 1. "send a lot of HTTP requests and update the table" <- elaborate. 2. pivotallabs.com/preemptive-optimization Commented May 8, 2014 at 16:43
  • 3. Use InnoDB,it has row-level locking instead of myISAM's table-level garbage. Commented May 8, 2014 at 16:49
  • "send a lot of HTTP requests and update the table" -> I have a bunch of user IDs, I need to call an API for every ID to obtain the information and then update my table. Those API calls will take time, not the saving ... Commented May 8, 2014 at 17:06

1 Answer 1

2

By just adding & at the end of command you can run any command in background. Here I'm redirecting o/p to /dev/null to avoid hang(For Linux).

exec('php do_job.php > /dev/null &');

If you want see the o/p of the command you can redirect it to a file.

exec('php do_job.php > full_path_to_file &');
Sign up to request clarification or add additional context in comments.

6 Comments

Except OP is on Windows.
I don't suppose this works on Windows: exec('php do_job.php > output.txt &');
Do I need to enable any particular PHP extension?
No..Also exec function will be enabled by default in XAMPP/WAMP
It's amazing ... I'm running the same code as above but the script hangs until the exec is done ...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.