Script to restart apache when running out of memory

Hi,

I have a problem with running out of memory in my Ubuntu web server (4GB RAM) because many people try to access my server.

I used to restart manually my apache server to clear out the memory & swap.

Can anyone tell me how to write bash script that can automatically restart apache when server get out of memory and trying to use swap or after using swap more than 500MB, for example?

Thanks

A cronjob can read the pid file, invoke ps, and then run the apache restart script if necessary. Alternatively, the cronjob can simply restart the webserver on a fixed interval.

More importantly, if your web server is consuming memory unboundedly, it is either misconfigured or buggy. If you don't know why it's happening, you should.

Regards,
Alister

Thanks Alister,

Instead of using cronjob, which schedule a task/script to be running based on pre-determined time, I want to restart the apache based on memory consumption. So, for example, if memory consumption reach 4GB, then restart the apache. I need a bash script to do this job.

Yes, I have to find the root cause of the problem but I need a temporary solution for this issue in order to keep the web server up.