I need a binary/script (php) that does the following.
Start n process of X in the background and maintain the number processes.
An example:
- n = 50
- initially 50 processes are started
- a process exits
- 49 are still running
- so 1 should be started again.
P.S.: I posted the same question on SV, which makes me probably very unpopular.
I would go in the direction that andres suggested. Just put something like this at the top of your pro.php file...
Have you tried making a PHP Daemon before?
http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/
Pseudocode:
If you translate this to
php
and fix its flaws, it might just do what you want.Can you use the crontab linux and write to a db or file the number of current process?. If DB, the advantage is that you can use to procedure and lock the table, and write the number of process.
But to backgroun you should use & at the end of the call to script
Here's something in Perl I have in my library (and hey, let's be honest, I'm not going to rig this up in PHP just to give you something working in that language this moment. I'm just using what I can copy / paste).
Whenever one of those systems calls finishes up, it moves on dequeing. Oh, and damn if I know hwy I did
0..$numthreads
instead of the normalmy $i = 0; $i < ...
idiom, but I did it that way that time.I have to solutions to propose. Both do child process reboot on exit, do child process reloading on USR1 signal, wait for the children exit on SIGTERM and so on.
The first is based on swoole php extension. It is very performant, async, non-blocking. Here's the usage example code:
The daemon code is here
Another is based on Symfony process library. It does not require any extra extensions. The usage example and daemon code could be found here