Networking

Unix and Linux network configuration. Multiple network interfaces. Bridged NICs. High-availability network configurations.

Applications

Reviews of latest Unix and Linux software. Helpful tips for application support admins. Automating application support.

Data

Disk partitioning, filesystems, directories, and files. Volume management, logical volumes, HA filesystems. Backups and disaster recovery.

Monitoring

Distributed server monitoring. Server performance and capacity planning. Monitoring applications, network status and user activity.

Commands & Shells

Cool Unix shell commands and options. Command-line tools and application. Things every Unix sysadmin needs to know.

Home » Apache, MySQL, Scripts

Restart Apache, MySQL When Low on Memory

Submitted by on June 25, 2009 – 4:27 am 17 Comments

Heavy traffic, hungry SQL queries, leaky applications will eventually leave your server low on memory. Apache will go on a hunt for swap space and MySQL will start freezing in thoughtful contemplation. Your hard drives will start scratching like a pack of homeless dogs and suddenly everything will become very very s l o w w w.

RAM is cheap these days, so if you can cram a couple more of these magic speed sticks into your system board, that would be thirty bucks well spent. However, if your server is maxed out and funding for an upgrade is months away, what you need is a script. It will be a very small script (after all, you are short on memory) that every once in a while will check the amount of free memory remaining and, if necessary, it will restart both the Apache and MySQL.

Hopefully, this would free up some RAM and your server will be good to go for a few more days. And so here is the script, which you can easily adapt to your specific unixoid OS.

#!/bin/bash

threshold=90 #percent
total=$(free | grep "Mem:" | awk '{print $2}')
remaining=$(free | grep "Mem:" | awk '{print $4}')
current=$(echo "scale=0;100-$remaining * 100 / $total" | bc -l)

if [ $current -gt $threshold ]
then
      /etc/init.d/apache2 stop
      /etc/init.d/mysql restart
      /etc/init.d/apache2 start

      echo "Restarted apache and mysql on `date +'%Y-%m-%d %H:%M:%S'`. RAM utilization at ${current}%" 
      >> /var/log/apache_mysql_restarter.log
fi

Put this script in the cron to run every couple hours (not too often). Use the log to see how severe is the memory problem. You may want to add the log to rotation to make sure it doesn’t grow too big.

Print Friendly, PDF & Email

17 Comments »

  • Not Found :(

    : command not foundne 2:
    (standard_in) 1: illegal character: ^M
    (standard_in) 1: illegal character: ^M
    : command not foundne 7:
    memoria-baja.sh: line 17: syntax error: unexpected end of file

  • Igor says:

    The “^M” is Windows end-of-line special character. You should be careful when you cut-and-paste the code. I suggest you use “vi” editor on the Unix/Linux system to check the code and made sure you get rid of the “^M” at the end of each line. Or download this file: https://www.krazyworks.com/wp-content/uploads/2009/08/memoria-baja.sh

  • Hi Igor

    Now if I work … Thank you very much: D

    Now I can sleep peacefully:)

    Thank you.

  • thanks Igor!

    it works for me, I could free up some RAM on my server.

    =)

  • Andrija says:

    Ha, ha, ha, very funy. Just implemented script, and found out that in order to look for REAL ammount of free memory, you need to check -/+ buffers, because my linux box was doing “disk cache” and my memory apperared completely exausted (and it was not, it was disk caching using unused RAM) and my apache was restarted EVERY minute. EVERY MINUTE !

    Do your homework, before publishing this kind of crap.

  • louisewoods1984 says:

    How can i secure my apache server to keep bad guys away?

  • gail C says:

    I’m doing a school project. We where given 6 words and we have to find out how to say them in a different language. Every word has to be a different language. I found the first five words. But the 6th word is flowers in the Apache language. The problem is, when I looked it up on the internet, I found out there are many different languages among the Apache tribes.

    I need the proper spelling and how to pronounce it.

    How can I find the answer to this?

  • Kobe says:

    I am not a computer course student, but I want to learn Apache, MySQL, and PHP. What are the softwares to use? Please suggest a plan I need to follow.
    I am planning to learn by self-study at home.
    Maybe running them and writting websites.
    I want to start the easiest of course.

  • simply complicated says:

    i have an apache 100 automatic atv quad bike i tried to start it this morning, the engine is turning over but it sounds like the fuel isnt getting through, i have checked the spark plug-which is fine, but it still wouldnt start. i tried jump-starting it from a lawnmower but still no success, i have nearly given up hope- please can somebody help me?

  • MAK & CHEESE says:

    If China obtains the apache or its technology, together with the wz-10 helicopter they currently owns, will their airforce be invincle in asia?
    I think if China gets the apache, it will further upgrade their ability to launch an attack on Taiwan.

  • Chris R says:

    Hello Everyone.

    I am taking a intro to linux class and I have a assignment to make a simple website with Apache. Does anyone know where I can find a website that will have a step by step on how to make one. All my website has to say is my name and what i think about Linux.

  • Matthew says:

    I have a Red Hat linux box (Server 1) serving as a gateway computer for our network, as well as hosting our website. I’m currently developing another website on another linux box (server 2) in the network, but connected to the internet through the other. Is it possible to have the gateway computer direct webbrowsers to server 2? Is there a configuration in apache that would do that?

  • Brian says:

    As title thanks. I am hoping to set up a forum and step 1 for me will be set up a server using Apache or IIS. Unfortunately I have little idea how to achieve that….

    (PS: I want to run this forum off my own computer acting as a server)

  • Lia-lu-li says:

    How can I join the army, and be the gunner of an apache or similar gunship. What courses, do I need previous training, and where could I sign up?
    Edit: I meant for America IF we go to war with them,
    Apache means like a Chopper gunner not a sniper. Cause I bet everyone wants to be one because they don’t get shot at/die as much, so I’m wondering if you have to go through more training?

  • Stevalicious says:

    How to change local host to my paid domain in Apache?
    I can’t message you!
    Even tho my domain is currently hosted I can just use it?
    @john I sent you a email.

1 Pingbacks »

Leave a Reply

%d bloggers like this: