Checking number of instances running

Discussion in 'Perl Misc' started by Rene Scheibe, Aug 7, 2003.

  1. Rene Scheibe

    Rene Scheibe Guest

    ....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
    Rene Scheibe, Aug 7, 2003
    #1
    1. Advertising

  2. Rene Scheibe

    pete Guest

    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
    pete, Aug 7, 2003
    #2
    1. Advertising

  3. "Rene Scheibe" <-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.
    Eric Schwartz, Aug 7, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gcash
    Replies:
    3
    Views:
    418
    Jordan Tucker
    Aug 3, 2003
  2. Guillaume Dargaud

    Checking for duplicate instances of a script...

    Guillaume Dargaud, Jan 21, 2004, in forum: Python
    Replies:
    5
    Views:
    236
    Josiah Carlson
    Jan 22, 2004
  3. John Wohlbier
    Replies:
    2
    Views:
    355
    Josiah Carlson
    Feb 22, 2004
  4. Replies:
    0
    Views:
    284
  5. Replies:
    8
    Views:
    444
    James Stroud
    Jan 29, 2009
Loading...

Share This Page