Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

killing child processes

by agoth (Chaplain)
on Jul 18, 2001 at 21:03 UTC ( #97768=perlquestion: print w/replies, xml ) Need Help??
agoth has asked for the wisdom of the Perl Monks concerning the following question:

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 ./ do
22315 /usr/local/bin/perl -w ./ -temail -lny -sa


#!/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
    fork() returns the PID of your child process. So it would be a lot simpler just to remember these values than to do a `ps`. But if you are doing a lot of exec, then that is not so useful cos exec() doesn't return; and if this script is a separate process, ditto.

    dave hj~

Re: killing child processes
by Malkavian (Friar) on Jul 18, 2001 at 21:31 UTC
    Unless I have the wrong end of the stick, a module that will do what you want is Proc::Simple.
    Have a peer on CPAN, and it's there. The documentation is simple, as is the implementation.
    The only caveat I have with Proc::Simple is that in exceptional circumstances it can segfault due to it's reaper code (signals are painful in Perl).


Re: killing child processes
by MZSanford (Curate) on Jul 19, 2001 at 15:40 UTC
    dash2 is definitly correct on exec(). If you call exec(), then no child process is created (see this for more info). If you are using fork() and then exec() immidiatle, then system() may be your friend.

    If you are using fork() without exec, then the fork funtion returns the PID of the child and kill($pid) should work when the time comes.
    Also, just a note, diffrent types of machines format ps in slightly diffrent ways, so if you stick with ps, make sure you test on the same type of machine as the production environment.
    OH, a sarcasm detector, thatís really useful

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://97768]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2017-05-23 23:05 GMT
Find Nodes?
    Voting Booth?