Win32::Job, wait until timeout, but capture all output

Hi,

I am writing a script that executes external command that may hang.
I want to capture all output produced by the external command and
continue with my perl code after a certain execution timeout for the
external program.
Since I am running the script on Windows, I cannot use any ALARM
mechanism.

The best alternative I could come up with is the use of Win32::Job, eg.

use win32::Job;

my $timeout = 10;
my $job = Win32::Job->new;
$job->spawn(undef, "iscsicli.exe", (), );
$job->run($timeout);

print "ok\n";


This particular test hangs for 10 seconds (because the iscsicli.exe
command without any arguments doesn't return) and then continues.

However, in my final script, I need to capture any output returned
by the external command, up to the point where the job gets killed,
and this has proven harder than it initially looked.

What I've tried so far:

- written to a temporary file by using this spawn syntax:

$job->spawn(undef, "iscsicli.exe", (), { stdout => "$$.output.txt" } );

but this creates an empty file, I think because of a lack of
autoflushing

- tried to redirect STDOUT to a variable before calling the
spawn() funtion, and enabling autoflush. Doesn't do the trick either,
and I don't know how to reset STDOUT to the default afterwards.

Could anyone please provide a working piece of code that
can execute an external command, wait for it to finish yet kill
the command if it takes too long, and still capture all the output
(STDOUT and STDERR) up to the last line before it got killed.
Ideally I would even like to do this without creating any temporary
files. Thanks in advance!

Best regards,
Filip

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Filip Sneppe [ Do, 03 Februar 2011 16:36 ] [ ID #2054530 ]

Re: Win32::Job, wait until timeout, but capture all output

On Feb 3, 7:36=A0am, filip.sne... [at] gmail.com (Filip Sneppe) wrote:
> Hi,
>
> I am writing a script that executes external command that may hang.
> I want to capture all output produced by the external command and
> continue with my perl code after a certain execution timeout for the
> external program.
> Since I am running the script on Windows, I cannot use any ALARM
> mechanism.
>
> The best alternative I could come up with is the use of Win32::Job, eg.
>
> use win32::Job;
>
> my $timeout =3D 10;
> my $job =3D Win32::Job->new;
> $job->spawn(undef, "iscsicli.exe", (), );
> $job->run($timeout);
>
> print "ok\n";
>
> This particular test hangs for 10 seconds (because the iscsicli.exe
> command without any arguments doesn't return) and then continues.
>
> However, in my final script, I need to capture any output returned
> by the external command, up to the point where the job gets killed,
> and this has proven harder than it initially looked.
>
> What I've tried so far:
>
> - written to a temporary file by using this spawn syntax:
>
> $job->spawn(undef, "iscsicli.exe", (), { stdout =3D> "$$.output.txt" } );
>
> but this creates an empty file, I think because of a lack of
> autoflushing
>
> - tried to redirect STDOUT to a variable before calling the
> spawn() funtion, and enabling autoflush. Doesn't do the trick either,
> and I don't know how to reset STDOUT to the default afterwards.
>
> Could anyone please provide a working piece of code that
> can execute an external command, wait for it to finish yet kill
> the command if it takes too long, and still capture all the output
> (STDOUT and STDERR) up to the last line before it got killed.
> Ideally I would even like to do this without creating any temporary
> files. Thanks in advance!

Not particularly elegant since output is sent to a file
via backticks but I succeeded in getting test program
output that occurs prior to a 3 second timeout, eg:

use strict;
use warnings;
use Win32::Job;

my $job =3D Win32::Job->new;
my $timeout =3D 3;

$job->spawn( $ENV{PERL},
'perl.exe -e "system q{c:/temp/test.pl>out.tmp 2>&1}"'
);
$job->run($timeout);
__END__

-----------------------------------------
test.pl:
#!perl
print "line1\n";
sleep 1;
warn "line1 to stderr\n";
sleep 5;
print "lastline\n";

-----------------------------------------
The output file contains:
line1
line1 to stderr

--
Charles DeRykus



--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
derykus [ Fr, 04 Februar 2011 21:23 ] [ ID #2054625 ]

Re: Win32::Job, wait until timeout, but capture all output

Hi,

On Fri, Feb 4, 2011 at 9:23 PM, C.DeRykus <derykus [at] gmail.com> wrote:

>
> Not particularly elegant since output is sent =A0to a file
> via backticks but I succeeded in getting test program
> output that occurs prior =A0to a 3 second timeout, eg:
>
> =A0 =A0use strict;
> =A0 =A0use warnings;
> =A0 =A0use Win32::Job;
>
> =A0 =A0my $job =3D Win32::Job->new;
> =A0 =A0my $timeout =3D 3;
>
> =A0 $job->spawn( $ENV{PERL},
> =A0 =A0 =A0 =A0 =A0 'perl.exe -e "system q{c:/temp/test.pl>out.tmp 2>&1}"=
'
> =A0 =A0 =A0 =A0 );
> =A0 $job->run($timeout);
> =A0 __END__
>
> -----------------------------------------
> test.pl:
> #!perl
> print "line1\n";
> sleep 1;
> warn "line1 to stderr\n";
> sleep 5;
> print "lastline\n";
>
> -----------------------------------------
> The output file contains:
> =A0 line1
> =A0 line1 to stderr
>
It works with your test program, but I cannot get it to work with the
iscsicli.exe command, unfortunately...

Best regards,
Filip

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Filip Sneppe [ Do, 17 Februar 2011 15:45 ] [ ID #2055417 ]

Re: Win32::Job, wait until timeout, but capture all output

On Feb 3, 10:36=A0am, filip.sne... [at] gmail.com (Filip Sneppe) wrote:
> Hi,
>
> I am writing a script that executes external command that may hang.
> I want to capture all output produced by the external command and
> continue with my perl code after a certain execution timeout for the
> external program.
> Since I am running the script on Windows, I cannot use any ALARM
> mechanism.
>
> The best alternative I could come up with is the use of Win32::Job, eg.
>
> use win32::Job;
>
> my $timeout =3D 10;
> my $job =3D Win32::Job->new;
> $job->spawn(undef, "iscsicli.exe", (), );
> $job->run($timeout);
>
> print "ok\n";
>
> This particular test hangs for 10 seconds (because the iscsicli.exe
> command without any arguments doesn't return) and then continues.
>
> However, in my final script, I need to capture any output returned
> by the external command, up to the point where the job gets killed,
> and this has proven harder than it initially looked.
>
> What I've tried so far:
>
> - written to a temporary file by using this spawn syntax:
>
> $job->spawn(undef, "iscsicli.exe", (), { stdout =3D> "$$.output.txt" } );
>
> but this creates an empty file, I think because of a lack of
> autoflushing
>
> - tried to redirect STDOUT to a variable before calling the
> spawn() funtion, and enabling autoflush. Doesn't do the trick either,
> and I don't know how to reset STDOUT to the default afterwards.
>
> Could anyone please provide a working piece of code that
> can execute an external command, wait for it to finish yet kill
> the command if it takes too long, and still capture all the output
> (STDOUT and STDERR) up to the last line before it got killed.
> Ideally I would even like to do this without creating any temporary
> files. Thanks in advance!
>
> Best regards,
> Filip

Let me know if you get any success. I tried IPC::Open2 and it didn't
work. I find that on WinXP, iscsicli works find to call is as a
command. i.e. `iscsicli ListPersistentTargets` but on Windows 7,
iscsicli is spawn another shell for it's output. So the output can't
be read from my program. Also, when I use open2 it hangs when I read.

Regards,

CEBundy


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Catherine Bundy [ Fr, 11 März 2011 07:30 ] [ ID #2056450 ]
Perl » gmane.comp.lang.perl.beginners » Win32::Job, wait until timeout, but capture all output

Vorheriges Thema: help with array elements
Nächstes Thema: Permission bit translator?