Create a bash script named ‘most_proc.sh’. This script should show the top 10 us
ID: 3799807 • Letter: C
Question
Create a bash script named ‘most_proc.sh’. This script should show the top 10 users according to the number of processes they are running. It should show the process count and the user name.
The output of the script should look something like this, depending on which users are active.
$ ./most_proc.sh
157 root
12 apache
11 brun1992
4 bb
2 postfix
2 hart4492
2 68
1 USER
1 sshd
1 rpcuser
I don’t care about the order of users with the same number of processes.
Some hints: 1) you will probably want to use ‘ps’ with certain options to see all processes of all users. 2) if you use “awk ‘{print $2}’ “ you will get the second column of text. 3) don’t forget some of the text processing commands we covered in lecture this week.
Explanation / Answer
most_proc.sh:
$> ps hax -o user |sort | uniq -c | sort -n -r | head -n 10
In above script,
ps - Lists processes
h in hax will remove header
-o prints only user column
sort will sort in alphabetically
uniq -c - counts each occurence for particular user and displays count of all occurences instead of separately dipalying each occurence
sort -n -r - prints the output in descending order by listing users with maximum number of processes first
head -n -10 will print only top 10 users