Nesh has asked for the wisdom of the Perl Monks concerning the following question:

Hi I am trying to run an executable by passing the name of exe with all the parameters to a subroutine. I don't have any idea how to check the status in perl. I am not sure about system() and exec() as they won't be returning the output. The code in vbscript is
Function ExecSyncStep(stepName) Dim WshShell, oExec Set WshShell = CreateObject("WScript.Shell") Set oExec = WshShell.Exec(stepName) Dim ouputStream ouputStream = "" Do While oExec.Status = 0 Do While Not oExec.StdOut.AtEndOfStream ouputStream = ouputStream & oExec.StdOut.ReadAll Loop WScript.Sleep 100 Loop Do While Not oExec.StdOut.AtEndOfStream ouputStream = ouputStream & oExec.StdOut.ReadAll Loop Set ExecSyncStep = oExec End Function
I came up with this
#Returns the status object of the sync process $oExec; $oExec = system($stepName); $ouputStream = ""; while($? eq 0){ Do While Not oExec.StdOut.AtEndOfStream ouputStream = ouputStream & oExec.StdOut.ReadAll Loop sleep 100; } Do While Not oExec.StdOut.AtEndOfStream ouputStream = ouputStream & oExec.StdOut.ReadAll Loop #WScript.StdOut.Write ouputStream return $oExec; }
Please advise.

Replies are listed 'Best First'.
Re: How to check a executable's status while running it.
by dragonchild (Archbishop) on Apr 18, 2005 at 19:02 UTC
    system() will set the return status in $?. Check it out. :-)

      It looks like the snippet checks whether a child is still running (by checking if the pipe is still open). system doesn't run a child in parallel, so your suggestion would help. It seems to me the solution would involve fork+exec (not recommended for Windows), system 1, ... (undocumented? Windows only), Win32::Process (Windows only), IPC::Open2, IPC::Open3, IPC::Run or IPC::Run3, but not system.

      I came up withj this
      #Returns the status object of the sync process $oExec; $oExec = system($stepName); $ouputStream = ""; while($? eq 0){ Do While Not oExec.StdOut.AtEndOfStream ouputStream = ouputStream & oExec.StdOut.ReadAll Loop sleep 100; } Do While Not oExec.StdOut.AtEndOfStream ouputStream = ouputStream & oExec.StdOut.ReadAll Loop #WScript.StdOut.Write ouputStream return $oExec; }
      Does this look right? But system won't return the output...
        open( CMD, "$command |" ) or die "Cannot open $command: $!\n"; while ( defined( my $line = <CMD> ) ) { # Do stuff with $line which is the output from $command } close CMD;
        Or, ikegami has a list of number of good modules that do similar things.