Hello Monks,
Like many others here, I'm trying to use Win32::Proc.
Here are my needs :
- 1 - launch a process
- 2 - get its PID
- 3 - capture its std_out and std_err
- 4 - watch it periodically, and react if the process is too long (the length is variable ; the reaction is not always to kill, but sometimes only warn, and let it run)
- 5 - get its return code once done
- 6 - being able to tell if the process has terminated by itself or was killed
There are various ways to launch a process and "monitor" it.
- - I don't use fork and exec because it doesn't create a process, but a thread in the same perl process
- - I don't use system because I want to do things while the process is running (basically, warn or kill if it's too long)
- - I don't use Proc::Background because it apparently can't get std_out and std_err
- - so for the moment, Win32::Process is what is closest to what I'm looking for :
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 :
- 1 - process is launched
- 3 - I can get std_out and std_err thanks to cmd /c but this prevents me from being able to get the real pid of my process being launched (sleep.exe in the example), and I can't kill it either ($child_proc->Kill(-1) will kill the cmd process, not the sleep.exe process)
- 4 - I can watch if it's too long or too short, but I can't kill
- 5 - I get the RC
What is not working :
- 2 - I get the PID of cmd.exe, not sleep.exe. If I remove cmd, I can't get the std_out and std_err in a file. I have been trying to redirect the STDOUT and STDERR in perl, but any print in my script will be mixed with my programs output, which is not what I want.
- 6 - I don't manage to tell whether the command ended by itself or was killed. Under unix, I can use fork() waitpid() and look into the $^E and $? variables. but here it's 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 !
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.