Using perl to watch other programs

Discussion in 'Perl Misc' started by Bill H, Nov 7, 2007.

  1. Bill H

    Bill H Guest

    Is there a way in perl to watch what is running on a server and close
    out applications that appear to be hung?

    I am running a perl bulletin board (YaBB) and have been having an
    issue where it seems to hang occasionally and not exit, causing server
    usage to go up to the point where the server locks up. While I am
    trying to determine what is causing this, I would like to do is be
    able to close an instance of the program if it is running for more
    than 1 minute, can this be done with a perl program (maybe running as
    a cron job)?


    Bill H
    Bill H, Nov 7, 2007
    #1
    1. Advertising

  2. Bill H

    Bill H Guest

    On Nov 7, 8:59 am, Bill H <> wrote:
    > Is there a way in perl to watch what is running on a server and close
    > out applications that appear to be hung?
    >
    > I am running a perl bulletin board (YaBB) and have been having an
    > issue where it seems to hang occasionally and not exit, causing server
    > usage to go up to the point where the server locks up. While I am
    > trying to determine what is causing this, I would like to do is be
    > able to close an instance of the program if it is running for more
    > than 1 minute, can this be done with a perl program (maybe running as
    > a cron job)?
    >
    > Bill H


    FYI this is on a linux server

    Bill H
    Bill H, Nov 7, 2007
    #2
    1. Advertising

  3. Bill H wrote:
    > On Nov 7, 8:59 am, Bill H <> wrote:
    >
    >>Is there a way in perl to watch what is running on a server and close
    >>out applications that appear to be hung?
    >>
    >>I am running a perl bulletin board (YaBB) and have been having an
    >>issue where it seems to hang occasionally and not exit, causing server
    >>usage to go up to the point where the server locks up. While I am
    >>trying to determine what is causing this, I would like to do is be
    >>able to close an instance of the program if it is running for more
    >>than 1 minute, can this be done with a perl program (maybe running as
    >>a cron job)?
    >>
    >>Bill H

    >
    >
    > FYI this is on a linux server


    You could have your Perl program scan the output of "ps aux" and kill
    all processes that have a runtime of more than 1 minute.
    However, as Linux boxen often live for lengthy times, there might be
    daemons which will fall into this category sooner or later, so you might
    need to restrict which commands the target processes must be or must not
    be running.

    omy @fn = qw (USER PID CPU MEM VSZ RSS TTY STAT START TIME COMMAND);

    if (open my $psuax, 'ps aux |') {
    while (<$psuax>) {
    next if /^USER/;
    my %info;
    @info{@fn} = split(/\s+/, $_, scalar @fn);
    next unless $info{TIME} =~ /^0:/;
    kill 9, $info{PID} if $info{COMMAND} =~ /client/;
    }
    close $psaux;
    }

    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Nov 7, 2007
    #3
  4. Bill H

    Ben Morrow Guest

    Quoth Bill H <>:
    > Is there a way in perl to watch what is running on a server and close
    > out applications that appear to be hung?
    >
    > I am running a perl bulletin board (YaBB) and have been having an
    > issue where it seems to hang occasionally and not exit, causing server
    > usage to go up to the point where the server locks up. While I am
    > trying to determine what is causing this, I would like to do is be
    > able to close an instance of the program if it is running for more
    > than 1 minute, can this be done with a perl program (maybe running as
    > a cron job)?


    The easiest way to do this is with setrlimit: then the OS will kill it
    for you.

    In general, you can use Proc::processTable to see what's going on with
    the other processes on the machine.

    Ben
    Ben Morrow, Nov 7, 2007
    #4
  5. Bill H

    Ted Zlatanov Guest

    On Wed, 07 Nov 2007 05:59:55 -0800 Bill H <> wrote:

    BH> Is there a way in perl to watch what is running on a server and close
    BH> out applications that appear to be hung?

    BH> I am running a perl bulletin board (YaBB) and have been having an
    BH> issue where it seems to hang occasionally and not exit, causing server
    BH> usage to go up to the point where the server locks up. While I am
    BH> trying to determine what is causing this, I would like to do is be
    BH> able to close an instance of the program if it is running for more
    BH> than 1 minute, can this be done with a perl program (maybe running as
    BH> a cron job)?

    Make your application send out a heartbeat, either by network multicast
    or by local IPC methods (the network multicast is generally better as it
    also allows for general monitoring). Put the process ID in the
    heartbeat packet. Send out every second or whatever.

    Your process watcher looks for running processes of interest, listens
    for heartbeats, and compares the heartbeat timestamp to the current
    time. If the difference is more than N seconds, kill the process.

    Ted
    Ted Zlatanov, Nov 7, 2007
    #5
  6. Bill H

    Bill H Guest

    On Nov 7, 10:14 am, Ben Morrow <> wrote:
    > Quoth Bill H <>:
    >
    > > Is there a way in perl to watch what is running on a server and close
    > > out applications that appear to be hung?

    >
    > > I am running a perl bulletin board (YaBB) and have been having an
    > > issue where it seems to hang occasionally and not exit, causing server
    > > usage to go up to the point where the server locks up. While I am
    > > trying to determine what is causing this, I would like to do is be
    > > able to close an instance of the program if it is running for more
    > > than 1 minute, can this be done with a perl program (maybe running as
    > > a cron job)?

    >
    > The easiest way to do this is with setrlimit: then the OS will kill it
    > for you.
    >
    > In general, you can use Proc::processTable to see what's going on with
    > the other processes on the machine.
    >
    > Ben


    What is setrlimit?

    Bill H
    Bill H, Nov 7, 2007
    #6
  7. Bill H

    Ben Morrow Guest

    Quoth Bill H <>:
    > On Nov 7, 10:14 am, Ben Morrow <> wrote:
    > >
    > > The easiest way to do this is with setrlimit: then the OS will kill it
    > > for you.

    >
    > What is setrlimit?


    man 2 setrlimit

    Your shell probably has a ulimit builtin that calls setrlimit, or you
    can call it from Perl with BSD::Resource. You will want to call it in a
    wrapper around the program that you're trying to apply the limit to;
    something like

    #!/bin/sh

    ulimit -t 70
    exec /path/to/real/binary

    Ben
    Ben Morrow, Nov 8, 2007
    #7
    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. Replies:
    12
    Views:
    1,615
    Dave Thompson
    Jan 10, 2005
  2. Chris
    Replies:
    1
    Views:
    1,958
    Christos TZOTZIOY Georgiou
    Apr 5, 2004
  3. Efialtis
    Replies:
    3
    Views:
    91
    John Bokma
    May 19, 2004
  4. Bernie Schneider

    Using Debugger Watch Expressions

    Bernie Schneider, Jul 13, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    129
    Bernie Schneider
    Jul 14, 2004
  5. Casey Hawthorne
    Replies:
    14
    Views:
    434
Loading...

Share This Page