-2

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.

Roshni Kasliwal
  • 229
  • 1
  • 11
Tri Nguyen
  • 99
  • 1
  • 1
  • 10
  • https://unix.stackexchange.com/a/434467/175321 – Pinke Helga Mar 04 '19 at 07:07
  • 4GB RAM is not that small. Have you monitored what is eating the memory? Consider to change from Apache to an alternative http(s) server. Also tuning the `swappiness` could help to keep the system responsive. – Pinke Helga Mar 04 '19 at 07:11
  • If there's an SSD, place the swap space there. – Pinke Helga Mar 04 '19 at 07:17
  • This server is a gateway between UI and another 3rd-party server, the 3rd-party server is usually slow and I can't control it so I guess the memory was eaten while waiting for responses from the 3rd-party server – Tri Nguyen Mar 04 '19 at 07:18
  • Try `top -o RES` at the command line. – Pinke Helga Mar 04 '19 at 07:21
  • Also consider reducing open connection by starting a background process pepending `nohup` to a system call to fetch 3rd-party's data. Let the client periodically ask via AJAX if the data is ready. https://en.wikipedia.org/wiki/Nohup A server reboot is a bad workaround. – Pinke Helga Mar 04 '19 at 07:37
  • It's posting data not getting data so restarting server with failed requests logged and re-requesting later is fine for me – Tri Nguyen Mar 04 '19 at 07:40

3 Answers3

2

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

Enoch
  • 921
  • 6
  • 15
1

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 using freeand gawk(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

Soulimane Mammar
  • 1,658
  • 12
  • 20
  • One should know how to interpret `free`. https://stackoverflow.com/questions/3784974/want-to-know-whether-enough-memory-is-free-on-a-linux-machine-to-deploy-a-new-app/4417121#4417121 – Pinke Helga Mar 04 '19 at 07:25
-1

You can write a cron which runs every 5s or any interval you think and does service httpd restart.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Santosh b
  • 729
  • 1
  • 8
  • 19
  • Note that this server runs intensive tasks and doesn't require an immediate response, it's fine waiting 20 seconds for the response, if I do so and the server is serving a request then the requests would fail, wouldn't it? In case the server takes forever to respond, I have a log system to logs every failed request and request again later but only for circumstances when the server fails. Restarting every 5 seconds would flood that log system. However, a cron job to check for memory and restart if needed is a good idea, thanks. – Tri Nguyen Mar 04 '19 at 06:57
  • 2
    That's a comment, not an answer. Well, how does he write the cronjob? That would transform it into an answer. – Pinke Helga Mar 04 '19 at 06:57