I hope it's not rude to answer my own question, but I hacked together a solution... 
any suggestions still welcome.

###################################
use Win32::Process;

my $server = "susny722";
my $tmpFile = "output.temp";
my $cmd = "uptime.exe \\\\$server \/a \/p:30";

print "calling $cmd...";

### save the standard output filehandle and open a new one
### reidrected to a file
open(THE_OLD_STDOUT, ">&STDOUT");
open( STDOUT, ">$tmpFile");

Win32::Process::Create( $oProcess,
                "C:\\aaa_test\\uptime.exe",
                $cmd,
                1,
                0,
                "." );

### it always takes at least 5 seconds, so wait that long before we
### start to loop
sleep(5);

### set our quit flags to false (0)
$flgDone = 0;
$flgFailed = 0;

$i=0;
while ( !$flgDone && !$flgFailed )
        {
        ### open the temp file to check it
        open( FILE, "$tmpFile");
        while (<FILE>)
                {
                ### use a counter to quit if we wait too long
                $i++;

                ### check for a match on the excpected last line
                if ( /Total Application Failures/ )
                        {
                        $flgDone = 1;
                        }
                ### if we waited for 60 seconds, flag a quit
                if ( $i > 60 )
                        {
                        $flgFailed = 1;
                        }
                }
        ### close the temp file and wait to open it again
        close(FILE);
        sleep(1);
        }

### retore the STDOUT and close the temp one
open (STDOUT, ">&THE_OLD_STDOUT") ;
close(THE_OLD_STDOUT);

### we did it, do whatever here (print for testing)
if ( $flgDone )
        {
        open( FILE, "$tmpFile");
        while (<FILE>)
                {
                print $_;
                }
        close(FILE);
        }

### the stupid thing hung again - KILL IT!!!!!!
if ( $flgFailed )
        {
        $oProcess->Kill( 0 );
        }

###################################

-----Original Message-----
From: Hornyak, Douglas 
Sent: Thursday, May 23, 2002 3:27 PM
To: [EMAIL PROTECTED]
Subject: Win32::Process::Create and STDIO


OK. I'm at my wits end... so let me pose the question to you all.

I need to create a process (calling the uptime.exe program). The previous programmer 
used system() but the exe keeps getting stalled and with system() there is no control 
over it. So I'm rewriting using Win32::Process::Create. However, this doesn't work:

###################################
use Win32::Process;

my $server = "servername";
my $tmpFile = "output.temp";
my $cmd = "uptime.exe \\\\$server \/a \/p:30 > $tmpFile";

#open (STDOUT, ">$tmpFile");
$fh = *STDOUT;

print "calling $cmd...";
Win32::Process::Create( $oProcess,
                "C:\\aaa_test\\uptime.exe",
                $cmd,
                1,
                0,
                "." );

print "process id is ". $oProcess->GetProcessID() . "\n";
print "Checking output file $tmpFile...";
###################################

the output file doesn't exist (is not created), and uptime.exe send me back it's usage 
info as if there were a syntax error. If I remove the redirection so that:

my $cmd = "uptime.exe \\\\$server \/a \/p:30";

the uptime.exe works fine. But, now I can't get the output of the uptime.exe program. 
It goes to the screen where I can watch it fly by 8^( I tried to use an old script of 
mine which was used on Linux with open3, but it just slips right through and leaves a 
zombie child process.

###################################
use Win32::Process;
use IPC::Open3;
use IO::Select;
use POSIX ":sys_wait_h";

my $server = "servername";
my $cmd = "uptime.exe \\\\$server \/a \/p:30 > $tmpFile";

# turn autoflush on
$| = 1;

$result = IssueCommand(1,  "uptime.exe \\\\$server \/a \/p:30" );
print $result;

sub IssueCommand {
        my $wait = shift;
        my $cmd = shift;

        ### run the command and catch all output
        my $pid = open3(*NSUP_IN, *NSUP_OUT, *NSUP_ERR, "$cmd");
        print "Command issued, pid is $pid\n";

        $SIG{CHLD} = sub {
                print "" if waitpid( $pid, 0) > 0;
                };

        close(NSUP_IN);

        $selector = IO::Select->new();
        $selector->add(*NSUP_ERR, *NSUD_OUT);

        while ( @ready = $selector->can_read )
                {
                foreach $fh ( @ready )
                        {
                        if ( fileno( $fh ) == fileno( NSUP_ERR ) )
                                {
                                $stderr = scalar <NSUP_ERR>;
                                }
                        else
                                {
                                $stdout = scalar <NSUP_OUT>;
                                }
                        $selector->remove($fh) if eof( $fh );
                        }
                }

        ### clean up
        close(NSUP_OUT);
        close(NSUP_ERR);

        $nscmd =~ s/\n/ /g;
        if ( $stderr eq "" )
                {
                print "Command $cmd succeeded, output was $stdout\n";
                return $stdout;
                }
        else
                {
                print "Command $cmd failed, error was $stderr\n";
                return $stderr;
                }

}
###################################

Any ideas?

p.s. Hey, Dave, did I miss this in chapter 8?



Visit our website at http://www.ubswarburg.com

This message contains confidential information and is intended only 
for the individual named.  If you are not the named addressee you 
should not disseminate, distribute or copy this e-mail.  Please 
notify the sender immediately by e-mail if you have received this 
e-mail by mistake and delete this e-mail from your system.

E-mail transmission cannot be guaranteed to be secure or error-free 
as information could be intercepted, corrupted, lost, destroyed, 
arrive late or incomplete, or contain viruses.  The sender therefore 
does not accept liability for any errors or omissions in the contents 
of this message which arise as a result of e-mail transmission.  If 
verification is required please request a hard-copy version.  This 
message is provided for informational purposes and should not be 
construed as a solicitation or offer to buy or sell any securities or 
related financial instruments.

_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Visit our website at http://www.ubswarburg.com

This message contains confidential information and is intended only
for the individual named.  If you are not the named addressee you
should not disseminate, distribute or copy this e-mail.  Please
notify the sender immediately by e-mail if you have received this
e-mail by mistake and delete this e-mail from your system.

E-mail transmission cannot be guaranteed to be secure or error-free
as information could be intercepted, corrupted, lost, destroyed,
arrive late or incomplete, or contain viruses.  The sender therefore
does not accept liability for any errors or omissions in the contents
of this message which arise as a result of e-mail transmission.  If
verification is required please request a hard-copy version.  This
message is provided for informational purposes and should not be
construed as a solicitation or offer to buy or sell any securities or
related financial instruments.

_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to