in reply to Re: Getting a Perl program to close a command it starts when the perl program exits?
in thread Getting a Perl program to close a command it starts when the perl program exits?

That's nearly perfect.
The only thing I'm seeing is that the tail is being kicked off through an sh. The process id of this is what's being returned to $tail_id and not the tail command itself.
If the tail command is killed it ends the shell.

Know how to stop the shell getting involved? Or getting the id of the command itself?

  • Comment on Re^2: Getting a Perl program to close a command it starts when the perl program exits?

Replies are listed 'Best First'.
Re^3: Getting a Perl program to close a command it starts when the perl program exits?
by ikegami (Patriarch) on Aug 29, 2007 at 17:09 UTC

    Don't use shell meta-characters (such as >) and the shell won't get involved. Or use IPC::Open3's open3 instead of open '...|'.

    One important difference between open and open3 is that open will call waitpid for you when the file handle is closed (implicitely or explicitely), while open3 won't. You have to call waitpid if you use open3 or you'll end up with zombies.

      I wasn't too bothered by the redirection, it was just to remove any error messages from tail.
      When it was removed all was well!

      Thanks for all this help, I've learned quite a bit. I never knew about the END block or that the redirection would get the shell involved. Thanks again.

Re^3: Getting a Perl program to close a command it starts when the perl program exits?
by BrowserUk (Patriarch) on Aug 29, 2007 at 17:07 UTC

    I don't use *nix, so this is something I've read about rather than done for myself, but...

    there is mention (in perlfunc open, maybe more in perlopentut?) of a more-than-3-argument form of the piped open. open FILEHANDLE,MODE,EXPR,LIST

    The idea is that you bypass the shell and run the command (tail in this case) directly, and so get the handle of the tail process returned rather than that of the shell. It would look something like:

    my $tail_id = open my $tail_fh, '-|', 'tail', '-1lf', 'info_file' or d +ie ...; END{ kill 3, $tail_id; }

    The caveat is that the shell is what performs the redirection of stderr in your example, so you cannot do that with this form of open.

    If that is a requirement then you are into using IPC::Open3 with all the complexities that entails.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.