I've written a simple Python script that monitors the number of file descriptors on a Red Hat system.
When comparing to the lsof
command I get two different results.
Broken down to it's core, the script does this:
import psutil
p = psutil.Process(PID)
print(p.num_fds())
Currently num_fds()
reports 60 open file descriptors.
While for the same PID the result of lsof -p PID | wc -l
yields 167.
Where is this discrepandy coming from?
My understanding was that both, num_fds()
and lsof
, both report the same file descriptors, including open file handles, sockets, pipes, etc.
Small background: some processes seem to open sockets and/or file handles without closing them ever again. Thus after a longer period of time the process reaches it's limit of file descriptors and crashes. This tool is intended to monitor this process wether the number of file desriptors is constantly rising.