Created
March 10, 2023 17:23
-
-
Save sbamin/ba45a66e2fd4ae0852df0aaee22d6782 to your computer and use it in GitHub Desktop.
Check CPU and Memory usage for a given user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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