13

Running Apache and Jboss on Linux, sometimes my server halts unexpectedly saying that the problem was Too Many Open Files.

I know that we might set a higher limit for nproc and nofile at /etc/security/limits.conf to fix the open files problem, but I am trying to get better output, such as using watch to monitor them in real-time.

With this command line I can see how many open files per PID:

lsof -u apache | awk '{print $2}' | sort | uniq -c | sort -n

Output (Column 1 is # of open files for the user apache):

1     PID
1335  13880
1389  13897
1392  13882

If I could just add the watch command it would be enough, but the code below isn't working:

watch lsof -u apache | awk '{print $2}' | sort | uniq -c | sort -n
tshepang
  • 12,111
  • 21
  • 91
  • 136
tesla-rules
  • 130
  • 1
  • 1
  • 5

2 Answers2

6

You should put the command insides quotes like this:

watch 'lsof -u apache | awk '\''{print $2}'\'' | sort | uniq -c | sort -n'

or you can put the command into a shell script like test.sh and then use watch.

chmod +x test.sh
watch ./test.sh
reader_1000
  • 2,473
  • 17
  • 15
4

This command will tell you how many files Apache has opened:

ps -A x |grep apache | awk '{print $1}' | xargs -I '{}' ls /proc/{}/fd  | wc -l

You may have to run it as root in order to access the process fd directory. This sounds like you've got a web application which isn't closing its file descriptors. I would focus my efforts on that area.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Gearoid Murphy
  • 11,834
  • 17
  • 68
  • 86