use POSIX; use File::Tee 'tee'; my $ctrl_c; my $outfile = "test.out"; my $errfile = "test.err"; $SIG{INT} = 'IGNORE'; tee(STDOUT, '>', $outfile) or die "Couldn't tee STDOUT to log file '$outfile': $!"; tee(STDERR, '>', $errfile) or die "Couldn't tee STDERR to log file '$errfile': $!"; $SIG{INT} = sub { $ctrl_c = 1; }; print "Testing 1.\n"; print STDERR "Test err 2.\n"; system("sleep 3 ; echo test 3; echo test 4 1>&2"); print "System: $?\n"; kill SIGINT, $$ if ($? & 127) == SIGINT; die "Interrupted!" if $ctrl_c; print "Done!\n";