#! perl -slw use strict; use IPC::Open3; use threads stack_size => 4096; ## Kill 0,pid does appear to work properly, ## It return true even after the executable is dead & gone sub checkPid{ scalar( `tasklist /nh /fi "pid eq $_[0]"` !~ m[INFO]sm ); } sub execute { my( $timeout, @command, ) = @_; my( $err, $in, $out ) = do{ local *FH; \*FH }; my $pid = open3( $in, $out, $err, @command ) or die "@command : $!/$@/$^E"; my( $Tout ) = threads->create( sub{ my( $Terr ) = threads->create( sub{ my @in = <$err>; return \@in } ); my @in = <$out>; return \@in, $Terr->join; } ); sleep 1 while checkPid( $pid ) and --$timeout; kill 3, $pid unless $timeout; return $Tout->join, $timeout == 0; } my @cmd = ( 'perl.exe', q[-le"$|++; print($_), warn( $_), Win32::Sleep 250 for 1 .. 12" ] ); my( $out, $err, $timeout ) = execute( 2, @cmd ); print 'Command ', $timeout ? 'timed out' : 'completed'; print "From stdout:[\n @$out ]" if @$out; print "From stderr:[\n @$err ]\n" if @$err; ( $out, $err, $timeout ) = execute( 5, @cmd ); print 'Command ', $timeout ? 'timed out' : 'completed'; print "From stdout:[\n @$out ]" if @$out; print "From stderr:[\n @$err ]" if @$err;