I was tasked with finding how many files were open for a specific process name. The process name had many process IDs (PIDs) running, which could be listed using the following command:
ps -eaf | grep -i<ProcessName>
The PIDs were listed in the second column, which the required the addition of one more command:
ps -eaf | grep -i $CheckProcessName | awk -F ' ' '{print $2}'
The output was a list of PIDs, in one column, and the amount of open files could be checked for each of them, by running the following command for the PIDs:
lsof -p <PID1>,<PID2>,<PID3>,...,<PIDn> | wc -l
This was cumbersome for processes that have multiple PIDs, thus adding one extra command to list all PIDs in one line:
ps -eaf | grep -i $CheckProcessName | awk -F ' ' '{print $2}' | tr '\n' ,
I decided to combine it all together and came up with this one-liner:
lsof -p $(ps -eaf | grep -i <ProcessName> | awk -F ' ' '{print $2}' | tr '\n' ,) | wc -l
Last, but not least, I created a shell script that could be used to check on any process:
#!/bin/bash
read -p "Enter process name: " CheckProcessName
lsof -p $(ps -eaf | grep -i $CheckProcessName | awk -F ' ' '{print $2}' | tr '\n' ,) | wc -l
Feel free to comment if you have a suggestion to make it simpler
Regards,
F. Bobbio C.