Linux – Auto restart server if virtual memory is too low

apache-2.2centos5drupal-6linuxvarnish

There are quite number of software running on my server: httpd, varnish, mysql, memcache, java..

Each of them is using a part of the virtual memory and varnish was configured to be allocated 3GB of memory to run.

Due to high traffic load which is 100K, our server ran out of memory and oom-killer is invoked. We've to reboot the server.

We have 8GB of Virtual Memory and due to some reason we cannot extend to larger memory.

My question is – Is there any automated script, which will monitor how much virtual memory left and based upon certain criteria, lets say if 500MB left than restart the server automatically?

I do know this is not the proper solution but we have to do it, otherwise we don't know when server will get OOM and by the time we know and restart the server, we lost our visiting users.

Best Answer

If I understand you correctly, you want something like the following:

  1. Check how much memory is left on the VPS.
  2. If 500M memory is left , reboot the VPS.

This could be done as follow

  1. Write a script that checks how much memory is left and reboot the VPS
  2. Add this script to crontab to automate the task.

e.g

#!/bin/bash

mem=$(free -m | awk '/Mem:/{print $4}')

(( mem <= 500 )) && reboot

Make the script executable

chmod +x scriptname // note don't add an extension

Add the script to the cron

crontab -e

* * * * * user_to_run_the_script /path/to/the/script

Hope you get the idea.