Restart Apache, MySQL When Low on Memory
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.
Popularity: 4% [?]
Related posts:
- Apache MySQL PHP Solaris 8 Installation
- MySQL Global Search and Replace Script
- Passing MySQL Commands from Shell Script
- Install PHP, Mysql, Apache2 on Solaris 9
- The correct MySQL GRANT syntax
- MySQL query cache
- MySQL LOAD DATA Syntax
- Find and Replace in MySQL
- Exporting spreadsheet data to MySQL
- Monitoring process CPU and memory usage



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
[Reply]
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: http://www.krazyworks.com/wp-content/uploads/2009/08/memoria-baja.sh
[Reply]
Hi Igor
Now if I work … Thank you very much: D
Now I can sleep peacefully:)
Thank you.
[Reply]
[...] KrazyWorks Etiquetas: apache, bash, grep, httpd, memoria, mysql, ram, reiniciar, script, [...]
thanks Igor!
it works for me, I could free up some RAM on my server.
=)
[Reply]
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.
[Reply]
admin Reply:
March 23rd, 2011 at 5:57 am
You have to admit that it was pretty silly of you to use this script on a system that uses Apache RAM cache. I guess you’ve learned your lesson. It’s a good idea to know what you are doing.
[Reply]
JustinFromNJ Reply:
June 25th, 2011 at 3:02 pm
Not only that, but you also specifically recommended to set up the cron job to run this script every few hours, not every minute.
Andrija, you’re a moron.
[Reply]