Nk_memory_by_users
Description
Get the total memory used by each user currently.
Example
[root@cloudvpsserver ~]# nk_memory_by_users User │ Mem-Used Mem-Percent │ root │ 412MB 22.5% │ nobody │ 101MB 5.50% │ telegraf │ 35MB 1.86% │ systuser │ 25MB 1.33% │ lwadmin+ │ 21MB 1.12% │ dovenull │ 17MB 0.91% │ mysql │ 14MB 0.74% │ wp-tool+ │ 8.9MB 0.48% │ nscd │ 5.8MB 0.31% │ named │ 4.2MB 0.22% │ cpanelc+ │ 3.8MB 0.20% │ dovecot │ 2.8MB 0.14% │ mailnull │ 2.3MB 0.12% │ polkitd │ 1.7MB 0.09% │ dbus │ 1.7MB 0.08% │ chrony │ 1.5MB 0.07% │
Code
nk_memory_by_users() { gen_body() { ps_snapshot_file="/root/.ps_snapshot" ps faux > "$ps_snapshot_file" mem_total="$(free -b | awk '/Mem:/ {print $2}')" gen_active_users() { awk '{print $1}' "$ps_snapshot_file" | sort | uniq | grep -v "USER" } echo "User │ Mem-Used Mem-Percent │" for user in $(gen_active_users); do mem_used="$(grep -E "^$user" $ps_snapshot_file | awk '{sum += $6} END {print sum*1000}')" mem_used_human="$(numfmt --to=iec --suffix=B "$mem_used")" mem_percent="$(nk_percent "$mem_used" "$mem_total")" echo "$user │ $mem_used_human $mem_percent │" done | sort -rh -k 3 } gen_body | column -t rm -f "$ps_snapshot_file" }