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

I'm using the backticks to run external unix commands. Everything's fine then the commands stop working and when I look at $! its reporting "Too many open files". Some more background, I'm using SIG{ALARM} to timeout the commands and SIG{CHLD} to reap the zombies. The problem seems to alway happen after 50 or more timeouts. I'm at a total stand still.

Replies are listed 'Best First'.
(tye)Re: backticks stop working
by tye (Sage) on Nov 09, 2001 at 02:32 UTC

    If you want to time out reading the output of an external command, then you really need to use     open(PIPE,"$command |")... and not backticks.

    Besides the problem you have already noticed, signals in Perl still aren't reliable and so have about a 2% chance of breaking Perl each time you use a Perl signal handler. So it doesn't take very many invocations of signal handlers before you have a pretty good chance of your script behaving unpredictably.

    A better solution would be to use select on a PIPE that was opened.

            - tye (but my friends call me "Tye")
      I changed the backticks to 'open' and all seem to be fine. thanks much.
Re: backticks stop working
by Fletch (Bishop) on Nov 08, 2001 at 22:50 UTC

    It sounds like perl isn't getting a chance to close the pipes from the backtick'd commands and is leaking file descriptors. You might be better off doing a fork/exec yourself, or perhaps using IPC::Open2 or IPC::Run and explicitly closing handles yourself.

      Are there any variables or something that I can look at to confirm this. I know there's the $^F, but it's of no value.

        If you've got something like lsof available you could use it to check from outside your program. Inside your program the only way I can think of offhand would be to open something (/dev/null or the like) and then use fileno on that handle and see if the file descriptors are steadily increasing (which would probably mean you've got a leak).