Using perl to watch other programs

B

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


Bill H
 
B

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

Bill H

FYI this is on a linux server

Bill H
 
J

Josef Moellers

Bill said:
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;
}
 
B

Ben Morrow

Quoth Bill H said:
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
 
T

Ted Zlatanov

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
 
B

Bill H

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
 
B

Ben Morrow

Quoth Bill H said:
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
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top