#!/usr/bin/perl use strict; use warnings; use POSIX ':sys_wait_h'; my $anzahl=5; my @kinder=map{$_=0}(0..$anzahl); my @cmds=("sleep 20", "sleep 10", "sleep 30", "sleep 10", "sleep 20", "sleep 30", "sleep 20", "sleep 10", "sleep 30", "sleep 10", "sleep 20", "sleep 30", "sleep 20", "sleep 10", "sleep 30"); sub ende() { $SIG{CHLD}='IGNORE'; kill(9,grep{$_>0}@kinder); exit; } $SIG{INT}=\&ende; $SIG{HUP}=\&ende; $SIG{QUIT}=\&ende; sub watch_childs() { for my $pid (grep{$_>0}@kinder) { if(waitpid( $pid, WNOHANG )==$pid) { print "Prozess $pid beendet"; my $status=$?/256; if($status!=0) { print " aber ein Fehler ist aufgetreten! ($?)\n"; $SIG{CHLD}='IGNORE'; kill(9,grep{$_>0}@kinder); die "Beende mich wegen Fehler in einem Kindprozess ($pid)!\n"; } else { print " und kein Fehler ist aufgetreten.\n"; } # nächsten Befehl staren $pid=0; if(@cmds) { my $cmd=shift(@cmds); $pid=fork(); exec($cmd) unless($pid); } } } } $SIG{CHLD}=\&watch_childs; for my $pid (@kinder) { my $cmd=shift(@cmds); $pid=fork(); exec($cmd) unless($pid); } # mach was anderes... my $ex=1; while($ex) { sleep(3); $ex=0; for my $pid (grep{$_>0}@kinder) { if(kill(0,$pid)) { print "Prozess $pid läuft noch. \n"; $ex=1; } } print "Es warten noch ".@cmds." Befehle auf Ausführung.\n"; }