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; }

In reply to killing child processes by agoth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.