I have a VPS hosted on AWS which runs PHP with httpd
. Usually, after a while the server (4GB RAM) is overloaded and takes forever to respond to requests, but doesn’t give any error. Restarting httpd
fixes this but since it doesn’t give any error, forever
cannot automatically restart it. How can I write to a script to restart httpd sudo service httpd restart
whenever the server memory falls below a particular number? Thanks.
3
Answers
You can write a cron which runs every 5s or any interval you think and does service httpd restart.
I guess from your question that you are using Ubuntu/Debian compatible OS.
The
free
command on Linux gives you the memory usage of your system. So you can write a bash script usingfree
andgawk
(to filter the necessary info from the output) to issue the restart command if whatever conditions on memory usage are met.Finally create a cron to periodically run your script
This is somewhat of a question with no real good answer because first of all you need to find out exactly what’s eating up all your RAM.
I have several VPS, some with as little as 2GB RAM, Running CWP.Pro, Firewalls, DNS and Mail Servers and HTTPD still never crashes after a lot of requests.
Please CONSIDER FIXING THAT.
In order to answer your question if you have Monit setup it can do what you want. It monitors your services and stops them after taking too much resources, restarts them when not running or there is an issue, etc.
Here is an excerpt to show what someone using it said:
When you cannot monitor your server for service availability, it is better to take help of automated monitor and restart utility. Last 4 days I was away from my server as I was enjoying my vacation. During this time due to load my lighttpd webserver died but it was restarted automatically within 2 minutes. I had utility configured for monitoring services on a Linux system called monit. It offers all features you ever needed for system monitoring and perform error recovery for UNIX like system.
To conclude all i’ve said here is the link to install and use Monit:
Monit 3rd Party
And the official site:
Official Site