in reply to Re: forking, waiting, and killing child pids
in thread forking, waiting, and killing child pids

You're sample of forks without & is very close to how I solved my problem, based on your original reply. I used Proc::ProcessTable to get the pid associated with snoop.

The following is a mock-up of pstree.

sshd,30982 --bash,30983 --su,31002 --bash,31003 --my.pl,6893 ./my.pl debug --sh,6934 -c ... --tcpdump,6935 --my.pl,6933 ./my.pl debug

I achieved these results by using open, as in "open, $snoop, $snoop_cmd". Then, while $snoop is running, I start a counter, and test whether the actual snoop pid is running.

while ( $counter < 31 ) { my $snoop_running = 1; for my $p ( @{ $ps->table } ) { if ( ( $p->pid == $snoop_pid ) && ( $p->ppid == getpgrp($fork_pid) ) && ( ( $p->cmndline ) =~ '$snoop_cmd' ) ) { $snoop_running = 0; print "packet capture still running (", $p->pid, ") ", $p->cmndline, "\n" if (( $debug eq 0 ) && (( $p->cmndline ) =~ '$snoop_ +cmd' )); } } if ( $snoop_running == 1 ) { #snoop must have finished already for my $p ( @{ $ps->table } ) { kill 9, ( $p->pid ) if ( $p->ppid == getpgrp($fork_pid +) ); } last; } $counter++; sleep 1;

I used this timer method, because at certain check points; 5, 10, and 20 seconds; I wanted to execute additional system commands, but I did not want to execute any if my snoop had already completed. Next, I kill the fork once packet capture is done. That level of fine pid control is admittedly overly fine, and not for everyone.

I don't think I would have figured this out, without your initial reply. Thank you very much!