Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Checking number of instances running (http://www.velocityreviews.com/forums/t881674-checking-number-of-instances-running.html)

Rene Scheibe 08-07-2003 10:44 AM

Checking number of instances running
 
....what do you think about checking the number
of scripts (with a specific name) running by:

my $processname = 'script.pl';
my @log = split /\n+/, `ps ax`;
my $count = 0;
foreach (@log)
{
chomp;
if (/$processname/)
{
$count++;
}
}

Or is there a better solution?
I need this to check at startup of the script
if the max. number of instances i want to allow
is already reached and then to exit new
instances right after this test.

Thanks...
....Rene


pete 08-07-2003 12:51 PM

Re: Checking number of instances running
 
Rene Scheibe wrote:

> ...what do you think about checking the number
> of scripts (with a specific name) running by:
>
> my $processname = 'script.pl';
> my @log = split /\n+/, `ps ax`;
> my $count = 0;
> foreach (@log)
> {
> chomp;
> if (/$processname/)
> {
> $count++;
> }
> }
>
> Or is there a better solution?
> I need this to check at startup of the script
> if the max. number of instances i want to allow
> is already reached and then to exit new
> instances right after this test.
>
> Thanks...
> ...Rene


You could do

my $processname = "script.pl";
$instances = `ps aux|grep $processname`;
exit if $instances;

That might be slightly faster

Pete

Eric Schwartz 08-07-2003 10:46 PM

Re: Checking number of instances running
 
"Rene Scheibe" <Rene.Scheibe@Stud.TU-Ilmenau.de> writes:
>> my $processname = "script.pl";
>> $instances = `ps aux|grep $processname`;
>> exit if $instances;

>
> this exits even if no instance of the script
> is running because "ps aux|grep script.pl"
> itself is shown as process. and using awk
> will slow down things i think.


$instances = `ps aux | grep $processname | grep -v grep | wc -l`
exit if $instances > $limit;

-=Eric
--
Come to think of it, there are already a million monkeys on a million
typewriters, and Usenet is NOTHING like Shakespeare.
-- Blair Houghton.


All times are GMT. The time now is 04:31 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.