I'm trying to put up a temporary band-aid while I work out a solution for a app's memory leak. What I wrote was a small bash
script that I put in the root on the server. This is what the script is suppose to do:
- Get the location it's run in
- Check to to see if the script is the crontab
- If not in the crontab add to the crontab to run every 5mins
- Test the memory and check to see if the % is above percent_allowed
- If above test then restart nginx & php-fmp services
memory_protect.sh
:
#!/bin/sh
cronfile=memory_protect.sh #NOTE THIS SHOULD DETECT IT'S SELF
path=pwd #this is the path to the file
percent_allowed=80 #this should be max memory before action
has_cron(){
#is the file in the cron?
return [[ crontab -l | egrep -v '^$|^#' | grep -q $cronfile ]] && return 1 || return 0
}
test_memory(){
memusage=`top -n 1 -b | grep "Mem"`
MAXMEM=`echo $memusage | cut -d" " -f2 | awk '{print substr($0,1,length($0)-1)}'`
USEDMEM=`echo $memusage | cut -d" " -f4 | awk '{print substr($0,1,length($0)-1)}'`
USEDMEM1=`expr $USEDMEM \* 100`
PERCENTAGE=`expr $USEDMEM1 / $MAXMEM`
#if it's above 80% alert
return [[ $PERCENTAG>$percent_allowed ]] && return 1 || return 0
}
if [[ has_cron -eq 0 ]]
then
#was not here so add
#run this script every 5 mins
*/5 * * * $path/$cronfile
fi
if [[ test_memory ]]
then
#clear some memory
/etc/init.d/nginx restart
/etc/init.d/php-fpm restart
fi
The memory test seems to work when I run that by it's self, but this in whole doesn't seem to be working.
Update
I needed to run dos2unix
on the file, but I also realized I have a return on the conditions of each function in the end.. so that was not going to work. Right now it seems to say that [[
on the if
statement is not found.
Update 2 Seems close, it's running the restarting of the services, but it's not putting the cron job in.. so I don't see it running
#!/bin/bash
cronfile=memory_protect.sh #NOTE THIS SHOULD DETECT IT'S SELF
path=pwd #this is the path to the file
percent_allowed=80 #this should be max memory before action
has_cron(){
#is the file in the cron?
#return 0 #returning this just to test should
#be the next line but it's not working
return 0
[[ crontab -l | egrep -v '^$|^#' | grep -q $cronfile ]] && return 1 || return 0
}
test_memory(){
memusage=`top -n 1 -b | grep "Mem"`
MAXMEM=`echo $memusage | cut -d" " -f2 | awk '{print substr($0,1,length($0)-1)}'`
USEDMEM=`echo $memusage | cut -d" " -f4 | awk '{print substr($0,1,length($0)-1)}'`
USEDMEM1=`expr $USEDMEM \* 100`
PERCENTAGE=`expr $USEDMEM1 / $MAXMEM`
#if it's above 80% alert
[[ $PERCENTAG -gt $percent_allowed ]] && return 1 || return 0
}
if [[ has_cron -eq 0 ]]
then
#was not here so add
#run this script every 5 mins
#crontab -e */5 * * * $path/$cronfile
cat <(crontab -l) <(echo "*/5 * * * $path/$cronfile") | crontab -
else
echo "cron present"
fi
if [ test_memory ]
then
#clear some memory
sudo /etc/init.d/nginx restart
sudo /etc/init.d/php-fpm restart
fi
It's close now I think to being corrected.
has_cron
seems to be the current place of issue. But when I ran it after once withhas_cron
returning a0
it didn't set the task either. – jeremy.bass Sep 21 '13 at 17:37crontab -e
to add to a user's crontab entry. Not sure how to do that from a script though, let me look it up. HERE: stackoverflow.com/questions/878600/… – slm♦ Sep 21 '13 at 17:48monit
– Rahul Patil Sep 21 '13 at 20:31