Created
November 28, 2012 06:48
-
-
Save laserpilot/4159478 to your computer and use it in GitHub Desktop.
MemoryLeakMurder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#Script to reboot application | |
#Can be used in conjunction with Lingon or a Cron to run on a periodic basis, e.g. every 2 minutes. | |
#Version 1.0 written by Daniel Mare from http://hintsforums.macworld.com/showthread.php?p=592991 | |
#Date: 02/08/2010 | |
#Modified by Blair Neal 11_27_2012 http://www.blairneal.com | |
#Helpful shell scripting additions from Patricio Gonzalez Vivo http://www.patriciogonzalezvivo.com/ | |
MEM_THRESHOLD=6 #INTEGERS ONLY If any process uses more MEM (in percentage of total MEM) than this threshold, the application will be rebooted | |
#Say you have 8gb of RAM, then a process that is using 2gb or more would be using 25% | |
#test is MEM usage is excessive | |
MEM_LINE=$(ps wwaxm -o %mem,command | head -2 | tail -1) #Output from ps showing the TOP app that is using the most memory | |
MEM_USAGE=`echo $MEM_LINE | sed 's/\ .*//'` #Strip off the numbers from above output | |
MEM_USAGE=${MEM_USAGE/\.*} #Truncate decimals - bash only compares integers TODO: incorporate this line into one above | |
if [ $MEM_USAGE -gt $MEM_THRESHOLD ] ; then | |
#echo "Original line: " $MEM_LINE | sed -e 's/.*\///' | |
MEM_PROCESS=`echo $MEM_LINE | sed -e 's/.*\///'` #this is the process name, used to kill the app later | |
MEM_FORMATTED=`echo $MEM_LINE | sed -e 's/.*\///' | sed 's/ -psn_[0-9]_[0-9]*/.app/' | sed 's/ //g'` #Get the name of process that triggered this alert, strip off the -psn and ID numbers and delete all spaces so it can be compared with your app name. Could be more elegant, but gets the job done! | |
echo "Memory leak threshold exceeded by this process: " $MEM_FORMATTED | |
echo $MEM_FORMATTED "It was using this percentage of memory: " $MEM_USAGE | |
if [ $MEM_FORMATTED == "Twitter.app" ]; then #make sure you're killing your own app, not something else important like a system process | |
echo "Closing Leaky App " $MEM_PROCESS | |
killall $MEM_PROCESS | |
else | |
echo "This is not the leaky process you're looking for!" #Your app wasn't the culprit | |
fi | |
else | |
echo "All is well! Your app is not using too much | |
fi | |
sleep 15 #Wait 15seconds (arbitrary) for everything to close out before restarting | |
#Now check and see if your app is closed out, and if it isn't then re-open it | |
if [ $(ps ax | grep -v grep | grep "Twitter.app" | wc -l) -eq 0 ] ; #Replace Twitter.app with your own app's name | |
then | |
echo "YourAppName not running. Opening..." | |
open /Applications/Twitter.app | |
else | |
echo "YourAppName running" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment