Last active
December 19, 2015 15:49
-
-
Save ryran/5979561 to your computer and use it in GitHub Desktop.
Bash function for making it quick & easy to inspect mem-usage of processes.
This file contains 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
#!/bin/bash | |
# Uses pgrep, ps, and awk to sum the RSS & VSZ of any process names or PIDs passed to it | |
function memsum { | |
if [[ -z $1 ]]; then | |
echo "memsum: requires at least one PID or [partial] process name" | |
return 1 | |
fi | |
local pid p | |
until [[ -z $1 ]]; do | |
if [[ $1 =~ ^[0-9]+$ ]]; then | |
[[ -d /proc/$1 ]] && pid[$1]=$(</proc/$1/comm) | |
else | |
for p in $(pgrep $1); do | |
pid[$p]=$(</proc/$p/comm) | |
done | |
fi | |
shift | |
done | |
[[ -z ${pid[*]} ]] && { echo memsum: no processes or PIDs matched; return 1; } | |
ps --no-header -orss,vsz ${!pid[*]} | | |
awk '{ rss+=$1; vsz+=$2 } END { printf "Total RSS: %.1f MiB (%.2f GiB)\nTotal VSZ: %.0f MiB (%.2f GiB)\n", rss/1024, rss/1024/1024, vsz/1024, vsz/1024/1024 }' | |
printf "\nProcesses matched: "; for p in ${!pid[*]}; do printf " ${pid[p]} ($p)"; done; printf "\n\n" | |
pidstat -ruh -p $(echo ${!pid[*]} | sed 's/ /,/g') 1 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage:
And again: