Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to get output from a spawned cmd prompt?

by Xhings (Acolyte)
on Jan 21, 2010 at 07:19 UTC ( #818653=perlquestion: print w/ replies, xml ) Need Help??
Xhings has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

This is my first post.

I am trying to get the output of an executable in a file. This worked fine with Win2003 but on win2008 server due to compatibility issues, I changed the settings to win2003 SP1 for the executable to run. Honestly I am working on 2008 for the first time.

I see that the out put is getting forked in a new cmd prompt and exits after execution.Is there a way I can get the o/p from this forked window?

Error from the below program in my output file is "child exited with value 0".I am opeing a Dir, running tool.exe for each of the file in side it and spooling output to my file.

Thanks Xhings

@files = readdir DH; open STDOUT, '> '.$Location2.'\ProcessedFilesInDir.txt'; foreach $file (@files) { $file1 = "tool.exe [- options]"; system ($file1) == 0 or die "system @args failed: $?"; if ($? == -1) { print "failed to execute: $!\n"; } elsif ($? & 127) { printf "child died with signal %d, %s coredump\n", ($? & 127), ($ +? & 128) ? 'with' : 'without'; } else { printf "child exited with value %d\n", $? >> 8; } } close STDOUT;

Comment on How to get output from a spawned cmd prompt?
Download Code
Re: How to get output from a forked cmd prompt?
by ikegami (Pope) on Jan 21, 2010 at 08:26 UTC

    First, Windows can't fork processes. It spawns new processes.

    The problem is that your error checking is buggy. You're reporting an error when none occurred.

    system($file1); if ($? == -1) { die("Can't execute command: $!\n"); } elsif ($? & 127) { die("Command died from signal ", ($? & 127), "\n"); } elsif ($? >> 8) { die("Command exited with error ", ($? >> 8), "\n"); }

    Update: While it's true that your error handling is buggy, it's not the cause of your problem. Could you please add the following lines after system (in either in your version or mine) and post the output.

    use Devel::Peek; Dump($?);

    I have a hard time believeing $? has a non-integer value or an integer value less than -1, but that's the only way you could get the results you report. This command will report quite precisely the value of $?.

      Follwoing is the o/p:
      SV = PVMG(0x9b2e8c) at 0x9b9d3c REFCNT = 1 FLAGS = (GMG,SMG) IV = 0 NV = 0 PV = 0 MAGIC = 0x9c4f5c MG_VIRTUAL = &PL_vtbl_sv MG_TYPE = PERL_MAGIC_sv(\0) MG_OBJ = 0x9b9d2c MG_LEN = 1 MG_PTR = 0x9c4f8c "?"

        Me bad. That should have been

        use Devel::Peek; 1 if $?; # mg_get Dump($?);

        But don't bother. The only way only what you said could happen is if system and $? are not returning the same thing like they are suppose to. I could ask you to show me a dump of both to confirm that or to show that what you said is wrong. Neither outcome would be productive.

        Since there's no reason to check both, pick one. (See snippets below.) If you then have problems with it containing the wrong value, we'll investigate why it has the wrong value then, knowing what value is obtained and what the value should be.

        system($cmd); if ($? == -1) { die("Can't execute command: $!\n"); } elsif ($? & 127) { die("Command died from signal ", ($? & 127), "\n"); } elsif ($? >> 8) { die("Command exited with error ", ($? >> 8), "\n"); }
        or
        my $rv = system($cmd); if ($rv == -1) { die("Can't execute command: $!\n"); } elsif ($rv & 127) { die("Command died from signal ", ($rv & 127), "\n"); } elsif ($rv >> 8) { die("Command exited with error ", ($rv >> 8), "\n"); }

        By the way, make sure you aren't using system and fork in the same program.

Re: How to get output from a spawned cmd prompt?
by tokpela (Chaplain) on Jan 21, 2010 at 10:16 UTC

    I would use the IPC::Run3 CPAN module to do this.

    The run3 routine will allow you to capture the STDOUT to a variable that you can then use to check the output

    run3($cmd, \$stdin, \$stdout, \$stderr);

      OP might also find Capture::Tiny useful.

      Just a something something...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://818653]
Approved by AnomalousMonk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-12-28 02:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls