Skip to content

Instantly share code, notes, and snippets.

@mwcz
Created November 8, 2016 03:08
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwcz/bfd96f094efeff1c31f682dd4e0a97ab to your computer and use it in GitHub Desktop.
Save mwcz/bfd96f094efeff1c31f682dd4e0a97ab to your computer and use it in GitHub Desktop.
A simple script to create graphs of memory usage of Linux processes.
#!/usr/bin/env bash
# usage: memlog.sh PID
# requires gnuplot and matplotlib (dnf install python2-matplotlib gnuplot)
PID=$1
LOG=./$PID.log
PNG=./$PID.log.png
echo recording memory usage for PID $PID
echo log file: $LOG
echo png file: $PNG
while true; do
ps --pid $PID -o pid=,%mem=,vsz= >> $LOG
gnuplot -e "set term png small size 800,600; set output \"$PNG\"; set ylabel \"VSZ\"; set y2label \"%MEM\"; set ytics nomirror; set y2tics nomirror in; set yrange [*:*]; set y2range [*:*]; plot \"$LOG\" using 3 with lines axes x1y1 title \"VSZ\", \"$LOG\" using 2 with lines axes x1y2 title \"%MEM\""
sleep 1
done
@svenove
Copy link

svenove commented Aug 8, 2019

How can I add a timestamp to use on the x axis?
A regular "HH:mm" would suffice... :)

@mwcz
Copy link
Author

mwcz commented Aug 8, 2019

The gnuplot docs and/or StackOverflow should be able to help with that. I don't know gnuplot very well or I would give it a shot.

@svenove
Copy link

svenove commented Aug 8, 2019

Thanks for the quick reply!
I found this: https://stackoverflow.com/questions/40908880/memory-monitoring-on-mac-or-linux/40909901#40909901

I’ll probably be able to “merge” that with your script. :)

@parikls
Copy link

parikls commented Nov 4, 2020

@svenove https://github.com/parikls/mem_usage_ui maybe this will help you also)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment