Bash – How to check ulimit usage

bashlinuxulimit

Is there any way to check the usage of the ulimits for a given user? I know that you can change ulimits for a single process when you start it up or for a single shell when running but I want to be able to "monitor" how close a user is to hitting their limits. I am planning on writing a bash script that will report back to statsd the current usage percentage. Specifically, I want to track:

  1. open files (ulimit -n)
  2. max user processes (ulimit -u)
  3. pending signals (ulimit -i)

What I want out is the percentage of usage (0-100).

Best Answer

Maybe this helps for the first question:

If you know the process IDs (PID) of the specific user you can get the limits for each process with:

cat /proc/<PID>/limits

You can get the number of opened files for each PID with:

ls -1 /proc/<PID>/fd | wc -l

And then just compare the value of Max open files with the number of open file descriptors from the second command to get a percentage.

Related Question