sylvest Posted January 11, 2017 Posted January 11, 2017 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"); print_r($output); 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 Quote
sylvest Posted January 26, 2017 Author Posted January 26, 2017 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 Quote
TCH-Dick Posted January 28, 2017 Posted January 28, 2017 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. Quote
sylvest Posted January 28, 2017 Author Posted January 28, 2017 Dick, 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 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.