Jump to content

Debugging "too Many Open Files" Error


Recommended Posts

I'm getting an error where my web page fails to do an fopen() function with the error message "too many open files". I suspect that the problem is that I am not closing some files that I should be, or that my program is getting stuck in a loop, opening files ad infinitum. But to debug this I would like to see a list of all the open files, so I can work out where they are being opened and where they ought to be closed. I understand that the shell command lsof is supposed to do this. I'm trying to call lsof, and am getting a "command not found" error. This is the code I'm using to call lsof:

$output = shell_exec("lsof 2>&1");



This is giving me the "command not found" error. I have tried using exec too, but couldn't work out how to capture the output.


1. What is the best way of finding out where files are being opened, and where they are not being closed, or getting a list of all the currently open files?

2. Should lsof exist on Total Choice Hosting servers?

3. If so, how do I call it successfully, and capture the output generated?

4. If not, how do I find out what all the open files are?


Thanks - Rowan

Link to comment
Share on other sites

  • 2 weeks later...

In my PHP script I'm tgrying to open a log file. I am getting the error "too many open file handles".


I need to know which files are open to detect why I am getting too many. I guess that files are not being closed properly or I'm stuck in a loop. I tried to use


$output = shell_exec('lsof 2>&1');
echo "<pre>$output</pre>";

and I'm now getting a very large number of "sh: lsof: command not found" messages.


Does the command lsof exist on TCH hosts?


Is this the right way to call it?


What is the best way to find out what files are being opened, to debug this type of error?


Thanks - Rowan

Link to comment
Share on other sites

Posting even part of your code in question might illicit more responses, without it everything is just going to be guesses.


Based on the fact that you are hitting a file handler limit and attempts to access lsof is returning a large amount of messages, I am guessing that it is stuck in an infinite loop. Since you are looping, trying to use lsof is not going to be of much benefit, as will result in further issues and excessive resources. With out reviewing all your code I would say start by insuring that fclose is not being called inside a conditional statement, which is probably a condition that is not currently being met. Also add a temporary counter/break option into the suspected loop, at the least this will limit the resources the script eats up and my help you track down the source.

Link to comment
Share on other sites



Thanks for your reply. I think I have spotted the problem now. As Bruce suggested, I think the function that opens files was trying to call itself (indirectly) open a log file when it detected an error, resulting in an infinite recursive loop with no end condition. I am trying to come up with a design that doesn't suffer from this problem now.


Thanks again. As before, I have found TCH support excellent - second to none, in my opinion.


Thanks - Rowan

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...