Skip to content

Instantly share code, notes, and snippets.

@olaseni
Last active May 2, 2019 05:17
Show Gist options
  • Save olaseni/891e74a14f214d64539e611439ffc5be to your computer and use it in GitHub Desktop.
Save olaseni/891e74a14f214d64539e611439ffc5be to your computer and use it in GitHub Desktop.
A simple bash script that dumps RAM metrics and usage.
#!/usr/bin/env bash
# RAM/CPU consumers
# ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head
# ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 20 # more details, less metrics
# ps aux --sort=-%mem | awk 'NR<=10{print $0}' # even more details
LOG="/var/log/ram.dump.log"
DATE="$(date)"
# echo -e "RAM Dump @ ${DATE} :\n $(vmstat -sn -SM | egrep '(total|free)')" >> ${LOG}
echo "RAM Dump @ ${DATE} : $(python -c 'import psutil; v=psutil.virtual_memory(); m=1024*1024; print "%sM total, %sM free" % (v.total/m,v.free/m)')" >> ${LOG}
echo -e "RAM/CPU Profile @ ${DATE} : \n $(ps -eo pid,ppid,user,cmd:100,%mem,%cpu --sort=-%mem | head -n 4)" >> ${LOG}
# additionally call `freemem` if free memory is less than 1000M
python -c 'import os,psutil; v=psutil.virtual_memory(); free=v.free/(1024*1024); os.system("/usr/local/bin/freemem" if free < 1000 else "true")' >> ${LOG}
# clear line
echo "" >> ${LOG}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment