2

Running debian jessie. The following is done as root to avoid any permissions issues. First I find the pid of my program, then I check its open file limit to verify that it is set at 1024. But when I count the number of open files, I find 1851.

$ pgrep -x process_name
673
$ prlimit --pid 673 --nofile
RESOURCE DESCRIPTION              SOFT HARD UNITS
NOFILE   max number of open files 1024 4096 
$ lsof -n -p 673 | grep -c "IPv4"
1851

For good measure, I check that the process doesn't have any subprocesses that it might be delegating file openings to:

$ pgrep -P 673
# no output

What is going wrong here?

nullUser
  • 236
  • 1
  • 7

1 Answers1

0

Because in the way you are using lsof, you are counting everything, not only the file descriptors, use something like this:

lsof -n  -p <pid_number> | awk '$5 ~ /([0-9].+)/'

But the more easy way is:

ls -l /proc/673/fd | wc -l 
c4f4t0r
  • 5,301
  • 3
  • 31
  • 42
  • Ok, I guess this technically answers my question. But then what do I need to limit in order to limit lines that match `$5 ~ /IPv4/`? – nullUser May 04 '15 at 23:53
  • why you need to filter IPV4? we the above command you filter all file descriptor for a process include ipv4 socket file descriptor. – c4f4t0r May 04 '15 at 23:57
  • Maybe you meant some other column besides `$5`? Which column were you trying to filter? Because `$5` for me is "Type" for which IPv4 definitely does not match `/([0-9].+)/`. – nullUser May 04 '15 at 23:58
  • in your question you told, you need to count the file descriptors, any using awk you can filter the output in every column you want, i used column and it's works, this is only for ipv4 network socket lsof -n | awk 'BEGIN {IGNORECASE=1;} $5 ~ /IPV4/' – c4f4t0r May 05 '15 at 00:09
  • Still has 1700 entries with that command. – nullUser May 05 '15 at 00:12
  • i updated my answer. – c4f4t0r May 05 '15 at 00:14
  • `ls -l /proc/673/fd | wc -l ` returns 1751 – nullUser May 05 '15 at 00:15
  • the soft limit is not a hard limit and user can change that number from his shell before run a program. – c4f4t0r May 05 '15 at 00:26
  • I changed the hard limit to 1024 also with `$prlimit --pid 673 --nofile=1024:1024` and it still exhibits the same behavior. – nullUser May 05 '15 at 00:45
  • because you process file descriptors are allocated, if you now restart the process, the process cannot allocate more than 1024 files. – c4f4t0r May 05 '15 at 08:32
  • If I restart the process won't it get a different pid and the changed limits be reset? – nullUser May 05 '15 at 14:22