Linux LVM: Reduce Filesystem and Logical Volume
March 14, 2012 – 5:07 pm | No Comment

The following example shows how to reduce the size of a filesystem mounted on an LVM logical volume. The instructions below are only for non-root filesystems. No reboot is required, but the filesystem will need to be unmounted. So, if there are any user applications using this filesystem, they will need to be stopped and the users will need to log out.

Read the full story »
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 am8 Comments
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:

  1. Apache MySQL PHP Solaris 8 Installation
  2. MySQL Global Search and Replace Script
  3. Passing MySQL Commands from Shell Script
  4. Install PHP, Mysql, Apache2 on Solaris 9
  5. The correct MySQL GRANT syntax
  6. MySQL query cache
  7. MySQL LOAD DATA Syntax
  8. Find and Replace in MySQL
  9. Exporting spreadsheet data to MySQL
  10. Monitoring process CPU and memory usage

8 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

    [Reply]

  • 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: 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]

  • 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.

    [Reply]

    admin Reply:

    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:

    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]

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.