use strict; #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'; 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 destroyed...\n"; setupProcess( $process ); } } sub shutdown( ) { $continue = 0; foreach my $pid ( keys %PIDPOOL ) { kill "HUP", $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 { $SIG{HUP} = sub{ print "Killing ssh...\n"; kill "ABRT", -$$; }; print "$process started...\n"; # replaceable with exec( ) or die(...) system( "/bin/sh -c 'ssh sporty\@tao \"tail -f t\" > t$process'") or die($!); exit; } }