agoth has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: killing child processes
by dash2 (Hermit) on Jul 18, 2001 at 21:18 UTC | |
|
Re: killing child processes
by Malkavian (Friar) on Jul 18, 2001 at 21:31 UTC | |
|
Re: killing child processes
by MZSanford (Curate) on Jul 19, 2001 at 15:40 UTC |