开发者

How to make sure PHP instance runs in background?

I have a ton of rows in MySQL. I'm going to perform a ping on an ip in each of these rows, so I'd like to split the load. I've made a script that runs a new process for every 100 row in the database. The problem is that the parent script seems to wait for each of the child scripts to finish before starting the next one, which voids the entire purpose.

This is the code of the important part of the parent script

for($i = 0; $i < $children_num; $i++)
{
    $start = $bn["dots_pr_child"] * $i;

    exec("php pingrange.php $i $start $bn[dots_pr_child]");
}

It's worth mentioning that each of these children processes run exec("ping") once per MySQL row. I'm thinking that's a part of the problem.

I'll post more information and code on request.

Is there a way to force the PHP instance to run in the background, and for the foreground to continue? Preferably the parent script should take 0.0001 sek and print "Done". Currently it runs for 120 seconds.

Thanks for any and all help


Edit: I've tried to add a & after the process. One would think that'd make the exec function return instantly, but nope.


Edit: Tried exec("php pingrange.php $i $start $bn[dots_pr_child] 2>&1 &"); without success


Edit: Tried exec("nohup php pingrange.php $i $start $bn[dots_开发者_Python百科pr_child] &"); without success


exec("php pingrange.php $i $start $bn[dots_pr_child] > /dev/null 2>/dev/null & ");

should do the work in background.


Try nohup and the ampersand:

exec("nohup php pingrange.php $i $start $bn[dots_pr_child] &");

The Wikipedia article on nohup gives a pretty good description of what it does.


While it might be overkill for this specific task, consider Gearman, a message / work queue designed for exactly what you're doing: farming out tasks to workers. It has comprehensive PHP support.

If you want to pass on Gearman for now, take a peek at proc_open instead of exec. It's a bit more complex, but it gives you a higher degree of control that might work better for you.


<?php
passthru("/path/script >> /path/to/log_file.log 2>&1 &");
?>

This should work since none of default PHP streams are used.


Just append > /dev/null 2>&1 & to your command, works for me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜