Usage ./graph.sh
It requires having gnuplot installed
| #!/bin/env bash | |
| # https://stackoverflow.com/questions/7998302/graphing-a-processs-memory-usage | |
| # trap ctrl-c and call ctrl_c() | |
| trap ctrl_c INT | |
| LOG=$(mktemp) | |
| SCRIPT=$(mktemp) | |
| IMAGE=$(mktemp) | |
| 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 "VSZ" | |
| 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 "VSZ" | |
| EOL | |
| function ctrl_c() { | |
| gnuplot $SCRIPT | |
| xdg-open $IMAGE | |
| exit 0; | |
| } | |
| while true; do | |
| ps -p $1 -o pid=,vsz=,rss= | tee -a $LOG | |
| sleep 1 | |
| done |
Great work. I made a few changes for some things I am working on and here are the results:
I found that
ps -prequired the PID so I usedpidofto get that. I added waiting so you can fire off the script before the executable runs and it will wait for it before beginning to log. Also added a command line argument for saving out the output file to somewhere other thanmktemp. I removedteeand used output redirection becauseteeoutput to both standard out and to the log file, just to clean things up. Also the loop ends, not onctrl-Cbut when the executable finishes running so that the whole lifetime is represented in the plot. Also implemented @will3216's suggestion because-o pid=,vsz=,rss=didn't work for me on CentOS 7 so I used the more generic syntax. Then I cleaned up the plot and used the same scale for both Real Memory and Virtual Memory and used the colors fromhtop.