in reply to Signals and subprocesses using fork, and system or exec.
This seems to work, substitituting a find command for the ssh:
use strict; use warnings; #Because `system' and backticks block `SIGINT' and #SIGQUIT', killing the program they're running #doesn't actually interrupt your program. $SIG{HUP} = \&shutdown; # $SIG{CHLD} = 'IGNORE'; # wait() doesn't see child die if this isn't +commented my %PIDPOOL = (); my $continue = 1; for( my $x = 1; $x < 3; $x++) { setupProcess( $x ); } while( $continue ) { my $pid = wait(); if( $pid > 0 ) { my $process = $PIDPOOL{ $pid }; print "Process $process ended...\n"; setupProcess( $process ); } } sub shutdown { $continue = 0; foreach my $pid ( keys %PIDPOOL ) { print "$pid\n"; kill 9, $pid; } print "Shutting down...\n"; while( ( my $pid = wait() ) != -1 ) { print "$pid killed on shutdown of parent process...\n" +; } } sub setupProcess { my $process = shift; my $pid = fork(); if( $pid ) { $PIDPOOL{ $pid } = $process; } else { print "$process, $pid started, ...\n"; close STDERR; # stop permission denied messages exec( q[find / -name ifconfig] ) or die($!); # exec( q[ssh bobn@trc1] ) or die($!); } }
Previous poster's comment on use of $SIG{CHLD} = 'IGNORE' and wait were correct.
When I use the ssh command shown, the individual ssh processes are not well killable, I assume because they are waiting for a password. I'm assuming you've set up 'RSA' authentication for your sessions.
As shown, killing the individual find processes causes new ones to spawn. In fact, even when they end naturally, new ones are spawned. Killing the parent process with -HUP kills everything.
--Bob Niederman, http://bob-n.com
All code given here is UNTESTED unless otherwise stated.
|
|---|