Skip to content

Instantly share code, notes, and snippets.

@sbamin
Created March 10, 2023 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbamin/ba45a66e2fd4ae0852df0aaee22d6782 to your computer and use it in GitHub Desktop.
Save sbamin/ba45a66e2fd4ae0852df0aaee22d6782 to your computer and use it in GitHub Desktop.
Check CPU and Memory usage for a given user
#!/usr/bin/env bash
## Check CPU and Memory Usage for an USER
## @sbamin
## https://superuser.com/a/920158/213756
## default to current user unless an user is specified as
## a first positional argument.
userid="${1:-"${USER}"}"
cpu="$(top -b -n 1 -u "${userid}" | awk 'NR>7 { sum += $9; } END { print sum; }')"
## https://unix.stackexchange.com/a/286759/28675
## Memory may not be an accurate given type of memory usage
## https://stackoverflow.com/a/21049737/1243763
mem="$(ps -U "${1:-"${userid}"}" --no-headers -o rss | awk '{ sum+=$1} END {print int(sum/1024) " MB"}')"
printf "INFO %s\t%s\thostname: %s\tcpu: %s\tmem: %s\n" "$(date +%Y%m%d_%H%M%S_%Z)" "${USER}" "$(hostname -s)" "${cpu}" "${mem}"
## end ##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment