Skip to content

Instantly share code, notes, and snippets.

@jsanta
Created July 13, 2018 19:26
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jsanta/de1614ce7d2cc79de891292b3253b2b5 to your computer and use it in GitHub Desktop.
Save jsanta/de1614ce7d2cc79de891292b3253b2b5 to your computer and use it in GitHub Desktop.
Script to restart PM2 procceses if memory exceeds limit
#!/bin/bash
memtotal=$(free | grep Mem | awk '{ print $2 }')
memuse=$(free | grep Mem | awk '{ print $3 }')
let "memusepercent = $memuse * 100 / $memtotal "
let "memtolerance = $memtotal * 0.9 "
echo "MemTotal: $memtotal (Usage tolerance: $memtolerance )"
echo "MemUsed: $memuse ($memusepercent %)"
if [ $memuse -ge $memtolerance ]; then
echo "Memory use over 90%"
pm2 restart all
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment