Your Location is: Home > Linux

Display users with at least 10 running processes on Linux terminal

From: Fidschi View: 3713 Ludvig Nobel 


How to write the names and number of processes to standard output on Linux terminal where only users with at least 10 running processes are shown?

Best answer

ps -eo user,cmd | awk '{ usr[$1]+=1;prc[$1][$2]="" } END { for (i in usr) { if (usr[i]>=10) { print i" - "usr[i];for (j in prc[i]) { print i" - "j }  } } }'

This can be achieved by using the -o option of ps to remove any "noise" and then piping the output through to awk.

Track the number of processes per user by creating an array usr and track the processes for each user by creating an 2 dimensional array prc. At the end, loop through the arrays, printing the count for each user where the process count is greater or equal to 10 followed by the actual processes.