Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Update: I have a simpler, cleaner version working now using Win32::Job. I like this version a lot; note that it relies on Windows 2000 and above.

use strict; use Win32::Job; select(STDERR);$|=1;select(STDOUT);$|=1; # autoflush my $SysDir = "$ENV{SystemRoot}\\system32"; # is there a better way? my $Outf = "out-$$.tmp"; my $Errf = "err-$$.tmp"; -f $Outf and (unlink($Outf) or die "error: unlink '$Outf': $!"); -f $Errf and (unlink($Errf) or die "error: unlink '$Errf': $!"); sub slurp_file { my $file = shift; local $/; open(my $fh, $file) or die "error:open '$file': $!"; <$fh>; } sub write_result { my ($pid, $rc, $elap, $user, $sys) = @_; warn "pid=$pid, rc=$rc, elapsed=$elap user=$user sys=$sys\n"; my $outstr = slurp_file($Outf); my $errstr = slurp_file($Errf); unlink($Outf) or die "error: unlink '$Outf': $!"; unlink($Errf) or die "error: unlink '$Errf': $!"; warn "cmd stdout='$outstr'\n"; warn "cmd stderr='$errstr'\n"; } # Run command $cmd, timing out after $timeout seconds. sub run_for { my ($cmd, $timeout) = @_; warn "run $cmd->[0] ($cmd->[1]) at " . scalar(localtime) . "\n"; my $job = Win32::Job->new(); defined($job) or die "error creating job: $^E"; my $pid = $job->spawn($cmd->[0], $cmd->[1], { stdin => 'NUL', stdout => $Outf, stderr => $Errf } ) or die "error spawn: $^E"; warn "in run_for, waiting for pid=$pid\n"; $job->run($timeout); my $stat = $job->status(); exists($stat->{$pid}) or die "oops, no status for $pid"; my $rc = $stat->{$pid}->{exitcode}; my $t = $stat->{$pid}->{time}; write_result($pid, $rc, $t->{elapsed}, $t->{user}, $t->{kernel}); } my @cmds = ( [ "$SysDir\\netstat.exe", 'netstat -na' ], [ $^X, 'perl -e "print STDERR Hello;sleep 15"' ], [ $^X, 'perl -e "print STDERR World"' ], [ "$SysDir\\cmd.exe", 'cmd /c DIR' ], ); for my $cmd (@cmds) { run_for($cmd, 10); }

In reply to Re: Timing Windows commands by eyepopslikeamosquito
in thread Timing Windows commands by eyepopslikeamosquito

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    [Discipulus]: ah thanks.. 1nickt all is well with the new job?

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (7)
    As of 2018-01-19 18:03 GMT
    Find Nodes?
      Voting Booth?
      How did you see in the new year?

      Results (222 votes). Check out past polls.