2

My situation is following: We run Magento Professional on a 15Gb ram instance, rackspace.

When running htop, we could see that 'apache2 -k start' keeps spawning more child processes, some of them eats ~900Mb of memory.

When memory is almost used up, all sites time out or become very slow. When memory is all used up, it seems that some of these processes got killed to free memory.

Then the same procedures happen.

My question is, could we setup ubuntu / apache to kill off child processes and free memory when memory usage reaches 85-90% ?

3
#!/bin/sh
TOTAL=`cat /proc/meminfo | grep MemTotal: | awk '{print $2}'`
USEDMEM=`cat /proc/meminfo | grep Active: | awk '{print $2}'`
LOG=/var/log/apache-free.log
LIMIT=90
DATA=`date +%Y-%m-%d" "%H:%M:%S`

if [ "$USEDMEM" -gt 0 ]
 then
    USEDMEMPER=$(($USEDMEM * 100 / $TOTAL))
    USEDMEMPERLOG=$USEDMEMPER
    if [ $USEDMEMPER -lt $LIMIT ]; then
                echo "$DATA | Memory: $USEDMEMPER%, (limit: $LIMIT%) | Do not restart!"
    else
                echo "$DATA | Memory limit reached ($LIMIT%): $USEDMEMPERLOG% | Restarting apache..."
                # restart apache
                sudo service apache2 restart
                TOTAL=`cat /proc/meminfo | grep MemTotal: | awk '{print $2}'`
                USEDMEM=`cat /proc/meminfo | grep Active: | awk '{print $2}'`
                USEDMEMPER=$(($USEDMEM * 100 / $TOTAL))
                echo "$DATA | Memory limit reached ($LIMIT%): $USEDMEMPERLOG% | Memory after restart: $USEDMEMPER%" >> $LOG
                tail -1 $LOG
    fi
fi

Save this code as apache-free.sh file and set as executable and add permissions

chmod +X apache-free.sh
chmod 755 apache-free.sh

add a crontab schedule, to run every 1 minute (feel free to redefine this time)

crontab -e

*/1 * * * * cd /dir/of/script && sh apache-free.sh

This script saves a log file in /var/log/apache-free.log with some informations about execution and memory saves.

** Remember: It's only a bandaid! It's necessary detect the real problem with your server.

I hope helps you!

2

It is not exactly what you asked for but you can use this method:

You must know two parameters: average memory usage per apache process, RAM amount you want to allocate for apache (for example full RAM - 2GB)

Then you set MaxClients directive to

MaxClients = RAM amount you want to allocate for apache (for example full RAM - 2GB) / average memory usage per apache process

So if I count your situation correct: 14GB (15-1 for system) / 800 => MaxClients ~= 18

But remeber: When you reach more traffic than 18 simultaneous requests, they will be queued, but your memory and whole SysOp will be safe...

0

This may be kludgy, but... you could set up a cron job that periodically run a script. Said script could check apache's memory usage, and kill / restart it if necessary.

This question has a cron example: https://stackoverflow.com/questions/5398014/cron-crontab-execute-a-script-every-minute-and-another-one-every-24-hours

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.