Skip to content

Instantly share code, notes, and snippets.

@coldfix
Created March 22, 2016 16:28
Show Gist options
  • Save coldfix/978c5dc5db88addace67 to your computer and use it in GitHub Desktop.
Save coldfix/978c5dc5db88addace67 to your computer and use it in GitHub Desktop.
Monitor RAM usage and runtime of a previously started process (using ugly hardcoding)
#! /usr/bin/zsh
if [[ $# = 0 ]]; then
echo "Usage: $0 PID [INTERVAL]"
exit
fi
pid=$1
pgid=$(ps -o pgid= $pid)
pgid=${pgid//[[:space:]]/}
interval=${2:-0.1}
_size() {
numfmt --to=iec-i --suffix=B --from-unit=1Ki --format="%10.1f" $1 |
sed -e 's/\([0-9.]\+\)\([a-zA-Z]\+\)/\1 \2/'
}
_cr="\r\033[K"
peak_rss=0
while info=($(/bin/ps -o pid=,rss=,etime= -$pgid)); do
# sum up current RSS from all process in the process group:
cur_rss=0
for i in $(seq 2 3 $#info); do
(( cur_rss += $info[$i] ))
done
# determine current elapsed wall time:
for i in $(seq 1 3 $#info); do
if [[ $info[$i] = $pid ]]; then
cur_time=$info[$i+2]
fi
done
# update peak value:
if (( $cur_rss > $peak_rss )); then
peak_rss=$cur_rss
fi
echo -n "${_cr}Time: $cur_time peak rss: $(_size $peak_rss) cur rss: $(_size $cur_rss)"
sleep $interval
done
echo "${_cr}Time: $cur_time peak rss: $(_size $peak_rss) FINISHED"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment