Good day monks. I have a bit of a head-scratcher that I haven't had much luck with so I hope that the collective mind of the monks can be of some help.

The problem is this, I have a script that starts with some filehandles already open which seem to be inherited via fork and/or exec and these file handles point to files not on the local filesystem which causes some pain when the remote file system goes out to lunch or the local system is rebooted. The annoyance is that I don't use these files in my script at all, so I'd just like to close all files other than STDIN, STDOUT and STDERR. Here's the code that was my attempt:

opendir(DH, "/proc/$$/fd"); while (my $item = readdir DH){ next if ($item =~ /\.\.?/); my @sinfo = stat("/proc/$/fd/" . $item); my $mode = $sinfo[2]; printf("Item is $item: %o\n", $mode); my $fh = FileHandle::fdopen($item, O_RDWR) || warn "Could not dup +$item"; print Dumper($fh); close($fh); } closedir(DH); opendir(DH, "/proc/$$/fd"); while (my $item = readdir DH){ next if ($item =~ /\.\.?/); print "Item is $item\n"; } closedir(DH);
This doesn't do the jobs since the 2nd opendir set prints out all of the same file descriptor numbers that exist at the beginning.

I'd be grateful for any suggestions.


In reply to Close all open file descriptors by bschmer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.