0

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.

5
  • 2
    Is there a part of it with which you're having problems, or is the question, "Will you do this for me?" If so, the answer is "No". Commented Aug 21, 2009 at 15:44
  • 2
    what will make you unpopular is making the answering of your question far more important than anyone elses. See catb.org/~esr/faqs/smart-questions.html#urgent Commented Aug 21, 2009 at 15:45
  • No absolutely not a "will you do this for me" thing. I just dont know where to start... Commented Aug 21, 2009 at 15:55
  • What the process X is ?? does it needs command line arguments ? What OS ? Commented Aug 21, 2009 at 16:34
  • @Paul: Thanks for your advice, I'll keep it in mind! And will read the FAQs now :). Commented Aug 25, 2009 at 9:35

6 Answers 6

1

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

# php-f pro.php &
Sign up to request clarification or add additional context in comments.

Comments

1

Pseudocode:

for (i=1; i<=50; i++)
  myprocess
endfor

while true
  while ( $(ps --no-headers -C myprocess|wc -l) < 50 )
    myprocess
  endwhile
endwhile

If you translate this to php and fix its flaws, it might just do what you want.

Comments

1

I would go in the direction that andres suggested. Just put something like this at the top of your pro.php file...

$this_file = __FILE__;
$final_count = 50;

$processes = `ps auwx | grep "php -f $this_file"`;
$processes = explode("\n", $processes);
if (count($processes)>$final_count+3) {
        exit;
}
//... Remaining code goes here

2 Comments

What's the 3 added to the final_count for?
1 is for a newline at the end of the explode. When I tested this, the command saw itself through the bash script, which gave me two additional entries. You can change the number to tweak it to get exactly 50.
1

Have you tried making a PHP Daemon before?

http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/

Comments

1

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).

#!/usr/bin/perl
use threads;
use Thread::Queue;

my @workers;
my $num_threads = shift;
my $dbname = shift;
my $queue = new Thread::Queue;

for (0..$num_threads-1) {
        $workers[$_] = new threads(\&worker);
                print "TEST!\n";
}

while ($_ = shift @ARGV) {
        $queue->enqueue($_);
}

sub worker() {
        while ($file = $queue->dequeue) {
                system ('./4parser.pl', $dbname, $file);
        }
}

for (0..$num_threads-1) { $queue->enqueue(undef); }
for (0..$num_threads-1) { $workers[$_]->join; }

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 normal my $i = 0; $i < ... idiom, but I did it that way that time.

1 Comment

I would have used fork/exec, and then wait() until one exits. Actually, I would have used Parallel::ForkManager. When I was an OpenMosix cluster admin, some users put it to good use, along with make -j n. There's also xargs -P n these days, as other ways to keep n processes in flight, but those are best for processing some set size of workload, not indefinite restarting.
0

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:

<?php
use Symfony\Component\Process\PhpExecutableFinder;

require_once __DIR__.'/../vendor/autoload.php';
$phpBin = (new PhpExecutableFinder)->find();
if (false === $phpBin) {
    throw new \LogicException('Php executable could not be found');
}
$daemon = new \App\Infra\Swoole\Daemon();

$daemon->addWorker(1, $phpBin, [__DIR__ . '/console', 'quartz:scheduler', '-vvv']);

$daemon->addWorker(3, $phpBin, [__DIR__ . '/console', 'enqueue:consume', '--setup-broker', '-vvv']);

$daemon->run();

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.