use strict; use warnings; use IO::Select qw( ); use IPC::Open2 qw( open2 ); use Time::HiRes qw( time ); # Optional my $timeout = 30.000; # Abort after this many seconds. my $command = '...'; # The command to execute. my $from_child = ''; # Receives the output of the command. my $pid = open2(undef, my $fh_from_child, $command); my $sel = IO::Select->new($fh_from_child); my $abort_time = time + $timeout; for (;;) { my $time_left = $abort_time - time; my @r = $time_left > 0 ? $sel->can_read($time_left): (); if (!@r) { kill(TERM => $pid); die("Child took more than $timeout seconds.\n"); } read($fh_from_child, $from_child, 4096, length($from_child)) or last; } print($from_child); waitpid($pid, 0);