Linux/Bash: How to report the CPU/ memory usage per user.
for user in `ps -aef | awk '{print $1}' | sort -k1 | uniq | grep -v UID`; do
procs=$(ps -u $user -o user,comm,vsz | wc -l) ;
echo -en $user " : Memory in KiB : " $(ps -u $user -o user,cmd,vsz | grep -v grep | awk 'BEGIN{s=0;} {s=s+$NF;}END{print s/1024}'); echo "" ;
echo -en $user " : CPU% : " $(ps -u $user -eo %cpu | grep -v grep | awk 'BEGIN{s=0;} {s=s+$NF;}END{print s}'); echo "" ;
done
Another method I learnt was:
ps -eo %cpu=,vsz=,user= |
awk '{ cpu[$3]+=$1; vsz[$3]+=$2 } END { for (user in cpu) printf("%-10s : Memory %10.1f KiB, CPU%%: %4.1f\n", user, vsz[user]/1024, cpu[user]); }'
OR
LC_ALL=C ps -eo %cpu=,vsz=,user= |
awk '{ cpu[$3]+=$1; vsz[$3]+=$2 } END { for (user in cpu) printf("%-10s : Memory %10.1f KiB, CPU%%: %4.1f\n", user, vsz[user]/1024, cpu[user]); }'
And finally:
ps axro "pid,%cpu,ucomm" | awk 'FNR>1' | head -n 3 | awk '{ printf "%5.1f%%,%s,%s",$2,$3,$1 }
ps axo "rss,pid,ucomm" | sort -nr | head -n3 | awk ‘{ printf "%8.0f MB,%s,%s",$1/1024,$3,$2 }
procs=$(ps -u $user -o user,comm,vsz | wc -l) ;
echo -en $user " : Memory in KiB : " $(ps -u $user -o user,cmd,vsz | grep -v grep | awk 'BEGIN{s=0;} {s=s+$NF;}END{print s/1024}'); echo "" ;
echo -en $user " : CPU% : " $(ps -u $user -eo %cpu | grep -v grep | awk 'BEGIN{s=0;} {s=s+$NF;}END{print s}'); echo "" ;
done
Another method I learnt was:
ps -eo %cpu=,vsz=,user= |
awk '{ cpu[$3]+=$1; vsz[$3]+=$2 } END { for (user in cpu) printf("%-10s : Memory %10.1f KiB, CPU%%: %4.1f\n", user, vsz[user]/1024, cpu[user]); }'
OR
LC_ALL=C ps -eo %cpu=,vsz=,user= |
awk '{ cpu[$3]+=$1; vsz[$3]+=$2 } END { for (user in cpu) printf("%-10s : Memory %10.1f KiB, CPU%%: %4.1f\n", user, vsz[user]/1024, cpu[user]); }'
And finally:
ps axro "pid,%cpu,ucomm" | awk 'FNR>1' | head -n 3 | awk '{ printf "%5.1f%%,%s,%s",$2,$3,$1 }
ps axo "rss,pid,ucomm" | sort -nr | head -n3 | awk ‘{ printf "%8.0f MB,%s,%s",$1/1024,$3,$2 }
1 Comments:
Mr Benjamin went above and beyond their requirements to assist me with my loan which I used to expand my pharmacy business,They were friendly, professional, and absolute gems to work with.I will recommend anyone looking for loan to contact. 247officedept@gmail.com.WhatsApp ... + 19893943740.
Post a Comment
<< Home