I have a script that runs, forks, and execs a number of child processes. I am not currently monitoring the children, I'll implement that later, so for testing I'm trying to parse the output of ps to give me a list of pid's with all the arguments so I can see which ones I'm about to kill off.
What I'm looking for is suggestions as to my kill handling and are there more efficient alternative to parsing ps output??
ps output:
22313 /usr/local/bin/perl -w ./b_mon.pl do
22315 /usr/local/bin/perl -w ./b_server.pl -temail -lny -sa
CODE
#!/usr/local/bin/perl -w
#-----------------------------------------
# process killer
#-----------------------------------------
use strict;
my %p_table = get_proc();
if (scalar keys %p_table >= 1) {
for (keys %p_table) {
my $pid = $_;
my $msg = "\nPID : $pid : PROCESS :: $p_table{$pid} ::: KILL i
+t ??? (y/n) : ";
print $msg;
while (<STDIN>) {
chomp;
if ($_ =~ /^y$/i) {
my $rc = kill 1, $pid;
($rc) ? print "killed OK\n" : print "didnt kill\n";
last;
} elsif ($_ =~ /^n$/i) {
last;
} else {
print $msg;
next;
}
}
}
} else {
print "No perl-ish processes found \n";
}
#--------------------------------------------------
# format output from ps into only perl processes
#--------------------------------------------------
sub get_proc {
my %table;
for (`ps -u richardh -o pid,args`) {
chomp;
if ($_ =~ /perl/) {
my @ary = split / /, $_;
my $var = shift @ary;
unless ($var == $$) {
$table{ $var } = join ' ', @ary;
}
}
}
return %table;
}