Search code examples
bashawkxargspsulimit

Getting the PID and read the limits file for each PID


I'm trying to get a list of PIDs and after printing the Max open files from the /proc/<PID>/limits file.

I found a way, but I was wondering if it's possible to do something more simple.

It will basically search for haproxy (sleep for this example :D), search for Max open files and finally read the limits' files for all process that was matched on the first awk.

The code is:

ps aux | awk '/sleep/ && !/awk/ { print $2 }' | xargs -I{} awk '/Max open files/{ print "PID="{}"\t\t"$0 }' /proc/{}/limits

The result will be something like this:

PID=16      Max open files            1048576              1048576              files
PID=17      Max open files            1048576              1048576              files
PID=18      Max open files            1048576              1048576              files
PID=19      Max open files            1048576              1048576              files
PID=20      Max open files            1048576              1048576              files
PID=21      Max open files            1048576              1048576              files
PID=22      Max open files            1048576              1048576              files
PID=23      Max open files            1048576              1048576              files
PID=24      Max open files            1048576              1048576              files
PID=25      Max open files            1048576              1048576              files
PID=26      Max open files            1048576              1048576              files
PID=27      Max open files            1048576              1048576              files

Solution

  • If you want to have it shorter, you might be able to do the following:

    for pid in $(ps h -o pid -C sleep); do
        echo "PID=$pid\t\t$(grep 'Max open files' /proc/$pid/limits)"
    done