Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Re: various questions about Win32::Process

by eyepopslikeamosquito (Chancellor)
on Jan 11, 2014 at 09:57 UTC ( #1070239=note: print w/replies, xml ) Need Help??

in reply to various questions about Win32::Process

Take a look at Win32::Job, a thin wrapper around Win32 Job objects. In the Win32::Job spawn() method, you can control stdin/stdout/stderr, while its run() method provides a simple way to run programs with a time limit. Another useful feature is that killing a job kills that process and all sub-processes spawned by it (similar to Unix process groups).

Sample code can be found at Re: Timing Windows commands.

  • Comment on Re: various questions about Win32::Process

Replies are listed 'Best First'.
Re^2: various questions about Win32::Process
by nabbo (Novice) on Jan 13, 2014 at 13:47 UTC

    This was the only module I had in mind, and did not take time to test it out before posting.

    The module is great, and solves easily most of my problems.

    Here is the code I wrote :

    use warnings; use strict; use Data::Dumper; use Win32::Job; my $cmd = "sleep 20"; my ($min_time,$max_time,$kill_time) = (12,15,30); my $i=1; my $max_sent = 0; my $job = Win32::Job->new; my $pid = $job->spawn(undef, $cmd, { stdin => 'NUL', stdout => 'stdou +t.log', stderr => 'stdout.log'}); print "PID = $pid\n"; my $ok = $job->watch("handler", 1); if($ok) { print "Done\n" ; } else { print "Problem !\n"; } my $status = $job->status; print Dumper($status); if($i<=$min_time) { print "MIN_TIME Too short...\n"; } sub handler { print "$pid still alive after $i sec\n"; $i++; if($i>$max_time and $max_sent==0) { print "MAX_TIME Too long...\n"; $max_sent = 1; } if($i>$kill_time) { print "KILL TIME : kill...\n"; return 1; } return 0; }

    Now... same issue as before. I don't manage to distinguish the fact that the process launched was killed externally, or finished with exit-code 1. Any idea about that ?

    Also, what if my perl process is killed (by the task manager for example) ? I tried :

    $SIG{INT} = sub { die "Caught a INT $!" }; $SIG{ABRT} = sub { die "Caught a SIGABRT $!" }; $SIG{FPE} = sub { die "Caught a SIGFPE $!" }; $SIG{ILL} = sub { die "Caught a SIGILL $!" }; $SIG{INT} = sub { die "Caught a SIGINT $!" }; $SIG{SEGV} = sub { die "Caught a SIGSEGV $!" }; $SIG{TERM} = sub { die "Caught a SIGTERM $!" };

    But none is actually called. Any idea ?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1070239]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-02-25 14:00 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (312 votes). Check out past polls.