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

Hello PerlMonks! I'm using IPC::Open3 module in development, everything is great but open3() returns pid of cmd.exe not of called application. i can't do waitpid/kill please help. thanks!

Replies are listed 'Best First'.
Re: IPC::Open3 and Real PID
by zentara (Cardinal) on Nov 19, 2009 at 16:56 UTC
    i don't know how it works for sure on windows, but a similar situation exists on linux, where you get the pid of the bash shell, not the executable eventually being run by bash.... see Killing children's of children

    ...there are a few workarounds.... use killfam ( or the win32 version) to kill the pid and all it's children.....or you can spawn a command shell with ipc, and print the name of the executable you want to run to the shell's stdin.... probably something like the Win32::Job module is what you want....see Kill a process in Perl and killing on win32 and A killall for Windows


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: IPC::Open3 and Real PID
by ikegami (Patriarch) on Nov 19, 2009 at 17:16 UTC

    When you launch a child process, Perl has no way of knowing what that process does once it starts. Specifically, if you launch a shell, it has no way of knowing what processes (if any) it will later launch.

    If you want an certain application to be the child, don't launch a shell instead. This can be achieved using the multiple argument form of open3.

    my $pid = open3( local *TO_CHLD, local *FR_CHLD, local *FR_CHLD_ERR, 'prog', $arg1, $arg2 );

    It's also less work to use the multiple argument form since it saves from converting $arg1 and $arg2 from text into shell literals.

    (I bet you didn't do that, so you probably have a bug and a security vulnerability in your code.)

      thanks for your response i use: my $pid = open3(...) So, what i need to do to kill spawned application?

        In unix, you'd do:

        kill TERM => $pid;

        I think that works in Windows too.

      Thanks. Everything works properly. I have not paid attention to multiple argument form. Really, if open3 called like open3($wtr,$rdr,$err,'command.exe -1 -2'); It produces cmd.exe.

        I have not paid attention to multiple argument form.

        I'm baffled. I told you that this is the solution to your problem, and you kept telling me it doesn't work even though you didn't even look at it.

        Really, if open3 called like open3($wtr,$rdr,$err,'command.exe -1 -2'); It produces cmd.exe.

        Yes. As previously explained, executing shell commands requires the shell. I also mentioned that applies to all OSes.