[SOLVED] is there a way to show top summarized by user?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i would like to see a display like top that shows a summary by user with one line per user and the heavier users on top. for each user this would have the sum of all processes by that user. is there some kind of configuration (i didn't a simple direct option in the man page) for top or some alternate software to do this?
I also looked into this and found what eight.bit.al found, a way to show output per user. I could not find a way to sort output by user.
It occurs to me that there might be a way to pipe the output of top to a script that in turn would then sort the output by user and store it in a file, but that would be only a snapshot, not a dynamic representation.
Sadly, I would be woefully unqualified to produce such a script.
just to be clear, i don't want to pick one user and show just that user's processes. i want to see which user is using the most resource.
a program that does this would get all the same data that top does, then collect a list of all the users. it would make an array with rows just like it did for processes. the it would loop through the array of processes, find the row for its real user in the user array, and add the resources to that row. the PID field would just be incremented by one to count how many active processes that user has. once all that is done, then sort the user array as appropriate and display it instead of the process array.
yeah, maybe i should get the source for top, add this code, add an option to engage this, and release the patch.
a program that does this would get all the same data that top does, then collect a list of all the users. it would make an array with rows just like it did for processes. the it would loop through the array of processes, find the row for its real user in the user array, and add the resources to that row. the PID field would just be incremented by one to count how many active processes that user has. once all that is done, then sort the user array as appropriate and display it instead of the process array.
awk is great at summarizing data like this. Since the structure of top's output lines is quite regular, it could serve as convenient input for a simple awk script.
Even more convenient might be the original data source under /proc, in particular values like /proc/PID/stat or /proc/PID/io. See the manual page.
#!/bin/sh
PATH=/usr/xpg4/bin:/usr/bin:/bin:/usr/sbin:/usr/ucb
export PATH
set -f
# secure GNU grep
unset GREP_OPTIONS
if [ -f /proc/cpuinfo ]; then
scaleps=`grep -c '^processor' /proc/cpuinfo` # ps pcpu is per vcpu
else
scaleps=1 # ps pcpu is per total
fi
#all in one variable:
allprocs=`ps -e -o pcpu= -o vsz= -o rss= -o user=`
#user= must be last in ps to not be truncated
#assume 900 processes maximum:
echo "#procs per user, top 3:"
echo "$allprocs" |
awk '{s[$4]++} END {for(i in s) printf " %-10s %-4d , %2d %%\n",i,s[i],int(s[i]*100/900)}' |
sort -nr -k 2,2 | head -3
#assume 6000 threads maximum:
echo "#threads per user, top 3:"
ps -Le -o user= |
awk '{s[$1]++} END {for(i in s) printf " %-9s %-4d , %2d %%\n",i,s[i],int(s[i]*100/6000)}' |
sort -nr -k 2,2 | head -3
echo "#cpu% per user, top 3:"
echo "$allprocs" |
awk '{s[$4]+=$1} END {for(i in s) printf " %-9s %2.1f\n",i,s[i]/c}' c=$scaleps |
sort -nr -k 2,2 | head -3
echo "#vmemoryMB per user, top 3:"
echo "$allprocs" |
awk '{s[$4]+=$2} END {for(i in s) printf " %-9s %4.1f\n",i,s[i]/1024}' |
sort -nr -k 2,2 | head -3
echo "#residentMB per user, top 3:"
echo "$allprocs" |
awk '{s[$4]+=$3} END {for(i in s) printf " %-9s %4.1f\n",i,s[i]/1024}' |
sort -nr -k 2,2 | head -3
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.