nabbo has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks,
Like many others here, I'm trying to use Win32::Proc.
Here are my needs :
There are various ways to launch a process and "monitor" it.
Here is what I have so far :
#!/usr/bin/perl use warnings; use strict; use Win32::Process; use Win32::Process 'STILL_ACTIVE'; my $child_pid; my $child_proc; my $cmd = 'sleep.exe 20'; my $debug = 1; my $exitcode = 1; my $stdout = 'out.txt'; my $stderr = 'err.txt'; Win32::Process::Create($child_proc, $ENV{COMSPEC}, "/c $cmd > $stdout +2>$stderr", 0, 0, ".") || print Win32::FormatMessage( Win32::GetLastE +rror()); $child_pid = $child_proc->GetProcessID(); my ($min_time,$max_time,$kill_time) = (12,15,18); if($child_pid!=0) { print "Started child process id $child_pid\n"; my $i=0; while ( 'true' ) { $child_proc->Wait(1000); $i++; $child_proc->GetExitCode($exitcode); if ( $exitcode == STILL_ACTIVE ) { print "loop $i\n"; if($i==$max_time) { print "max time : this is quite long...\n"; } if($i==$kill_time) { print "kill time : kill $child_pid\n"; $child_proc->Kill(-1); } } else { print "### E = $^E ?=$? ###\n"; print " Process terminated on it's own rc=$exitcode \n"; if($i==$min_time) { print "min time : command was too short\n"; } exit $exitcode; } } } else { print "Can't start process...\n"; }
What is working :
What is not working :
I have seen many threads about redirecting STDOUT and STDERR for the Win32::Process module, but never complete answer about the drawbacks (ability to kill and get real PID)
I'm using 32bits strawberry perl on a 64bits win2k8R2 box. My code needs to work on windows 2003/2008/2012. I can install some CPAN package by hand (the server is not connected to internet, whereas my workstation is)
I have tried several modules like Proc::Background Proc::Simple Proc::Reliable, with no luck.
Any help much appreciated. If some module can help, please tell me
Thank you very much for reading, please forgive my poor english.
Happy new year to you, perl Monks !
|
|---|