Checking number of instances running

R

Rene Scheibe

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

pete

Rene said:
...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
 
E

Eric Schwartz

Rene Scheibe said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,187
Members
46,730
Latest member
AudryNolan

Latest Threads

Top