Recently, my wibesite often meet Out of Memory Problem. And apache will be automatically killed the memory is full. So I decided to add a cron service to moniter the memory usage and restart the apache service.

#!/bin/sh
TOTAL=`cat /proc/meminfo | grep MemTotal: | awk '{print $2}'`
USEDMEM=`cat /proc/meminfo | grep Active: | awk '{print $2}'`
LOG=/tmp/test.log
echo > $LOG
if [ "$USEDMEM" -gt 0 ]
 then
     USEDMEMPER=$[$USEDMEM * 100 / $TOTAL ]
     echo "Current used memory = $USEDMEMPER %"
     if [ "$USEDMEMPER" -gt 90 ]; then
         killall -9 apache2
         service apache2 restart
         echo "apache process killed " >> $LOG
     fi
fi
cat $LOG 


crontab -e
*/5 * * * * sudo /home/ubuntu/apache-monitor.sh

This will set the shell script run every 5 minutes.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *