in reply to IPC::Open3 and Real PID

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.)

Replies are listed 'Best First'.
Re^2: IPC::Open3 and Real PID
by nusoff (Initiate) on Nov 19, 2009 at 17:39 UTC
    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.

        on windows $pid contains process id of cmd.exe :(
        yeah, i have tried your code but IPC::Open3::open3 calls cmd.exe /x/d/c "COMMAND" and returns pid of cmd.exe i dunno why.
        sorry. on Windows, IPC::Open3 works through cmd.exe and if i killing process with pid returned by open3, called process stays alive. my solution is system("taskkill /f /IM child.exe"); :(
Re^2: IPC::Open3 and Real PID
by Anonymous Monk on Nov 20, 2009 at 16:41 UTC
    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.