Skip to content

Instantly share code, notes, and snippets.

@wblondel
Forked from nicolasazrak/README.md
Last active September 18, 2019 14:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wblondel/bf07d21d47342f4e009b2b17097060a3 to your computer and use it in GitHub Desktop.
Save wblondel/bf07d21d47342f4e009b2b17097060a3 to your computer and use it in GitHub Desktop.
Graph process memory
#!/bin/sh
# https://stackoverflow.com/questions/7998302/graphing-a-processs-memory-usage
# Usage ./graph.sh <pid>
# It requires having gnuplot installed
# trap ctrl-c and call ctrl_c()
trap ctrl_c INT
LOG=$(mktemp)
SCRIPT=$(mktemp)
IMAGE=image.jpg
echo "Output to LOG=$LOG and SCRIPT=$SCRIPT and IMAGE=$IMAGE"
cat >$SCRIPT <<EOL
set term png small size 800,600
set output "$IMAGE"
set ylabel "RSS"
set y2label "%CPU"
set ytics nomirror
set y2tics nomirror in
set yrange [0:*]
set y2range [0:*]
plot "$LOG" using 3 with lines axes x1y1 title "RSS", \
"$LOG" using 2 with lines axes x1y2 title "%CPU"
EOL
function write_image() {
gnuplot $SCRIPT
open $IMAGE
exit 0;
}
function ctrl_c() {
kill -9 $APP_PID
write_image
}
exec $1 & export APP_PID=$!
while ps -p $APP_PID > /dev/null; do
ps -p $APP_PID -o pid=,%cpu=,rss= | tee -a $LOG
sleep $2
done
write_image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment