#! perl -slw use strict; use threads; use Thread::Queue; sub captureWithTimeout { my( $cmd, $Q, $timeout ) = @_; my $pid = open CMD, "$cmd |" or die "$cmd : $!"; sleep $timeout; my $rv; $rv = kill 9, $pid if kill 0, $pid; $Q->enqueue( $_ ) while defined( $_ = ); return $rv; } my $Q = new Thread::Queue; my $t = threads->create( \&captureWithTimeout, $ARGV[ 0 ], $Q, $ARGV[ 1 ] ); print "Running '$ARGV[ 0 ] for $ARGV[ 1] seconds"; print "It took too long, so I killed it" if $t->join; print 'It produced the following output:'; printf $Q->dequeue while $Q->pending; __END__ P:\test>435776 "perl test.pl 15" 10 Running 'perl test.pl 15 for 10 seconds It took too long, so I killed it It produced the following output: Starting 1 2 3 4 5 6 7 8 9 P:\test>435776 "perl test.pl 15" 20 Running 'perl test.pl 15 for 20 seconds It produced the following output: Starting 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Stopping