#!/usr/bin/perl use warnings; use strict; $SIG{CHLD} = 'IGNORE'; # to make sure there are no zombies sub run_shell(@) { my $pid = open my $pipe, "-|"; die "can't open forked pipe': $!" unless defined $pid; if ($pid) { # parent return { pid => $pid, pipe => $pipe }; } # child open STDERR, ">&STDOUT" or die "can't dup 2>&1: $!"; setpgrp; # move into its own process group exec @_; # give control of child to command die "exec @_ failed: $!"; # should never get here } my $subshell = run_shell qw(./forkage.sh); my $output_from_subshell = $subshell->{pipe}; my $lines_left = 20; print while defined($_ = <$output_from_subshell>) and $lines_left--; kill "HUP", -$subshell->{pid};