Skip to content

Instantly share code, notes, and snippets.

@vdakalov
Last active June 1, 2018 14:18
Show Gist options
  • Save vdakalov/62898b17dc15c2a166a7a843d1938cc9 to your computer and use it in GitHub Desktop.
Save vdakalov/62898b17dc15c2a166a7a843d1938cc9 to your computer and use it in GitHub Desktop.
Return total memory usage of processes by name
#!/bin/bash
###
# Save as /etc/bash_completion.d/mem.complete.sh
##
_complete_mem() {
commands=`ps --no-headers -e -o command | grep -Eo "^[a-zA-Z0-9_\.\-\/]+" | { while read line; do basename $line; done; } | sort -u`
current="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "${commands}" "${current}") )
}
complete -F _complete_mem mem.sh
#!/bin/bash
###
# Save as ~/bin/mem.sh
##
selfname=`basename $0`
name=$1
if [ -z "${name}" ]; then
echo -e "Display process total memory usage in kilobytes\n"
echo -e "Usage: ${selfname} NAME\nWhere:"
echo -e " \e[1mNAME\e[0m is process name (used as argument for pgrep)"
exit 0
fi
pids=`pgrep "${name}"`
if [ -z "${pids}" ]; then
echo -e "pids of ${name} is not found. test you process name: \e[1mpgrep ${name}\e[0m."
exit 0
fi
pidsCom=`echo "${pids}" | paste -sd "," -`
rssRaw=`ps --no-headers -p "${pidsCom}" -o rss`
rssSum=`echo "$rssRaw" | paste -sd "+" -`
rssTotal=$(($rssSum))
echo "${rssTotal} Kb"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment