LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Script to execute command if memory usage is over + a question (http://www.linuxquestions.org/questions/linux-general-1/script-to-execute-command-if-memory-usage-is-over-a-question-4175446353/)

ps1x0 01-19-2013 12:25 PM

Script to execute command if memory usage is over + a question
 
I need a script, that executes a command if memory usage on the machine is over 60%. I have a problem with apache and there's a memory leak which I can't locate. And that's the second question. How could I locate which process is causing the leak?
At least twice a day memory usage goes up to 100% until I dont reload or restart the httpd.
The script I need is not a real solution, but it should work.. :)

btmiller 01-19-2013 12:45 PM

My best advice would be to write a shell script to do what you want, and then at the top put a test to see how much memory is used and exit if memory usage is OK, e.g.:

Code:

#!/bin/bash

threshold=256 # replace this with the max amount of non-buffer/cache memory you expect to be used in MB
mbused=`free -m | grep "^-/+" | awk '{print $3}'`
if [ "$mbused" -lt "$threshold" ]; then exit; fi

## perform whatever action you want to take when mem use is above normal here

Then run this script from cron every 1-5 minutes at your discretion.

It's not terribly clean, but it's the best way I know to accomplish what you want to do.

unSpawn 01-19-2013 03:18 PM

Quote:

Originally Posted by ps1x0 (Post 4873567)
I have a problem with apache and there's a memory leak which I can't locate. (..) How could I locate which process is causing the leak?

Start by collecting system statistics using any SAR like Atop, Collect, Dstat, Atsar or just plain sysstat. Having that data will show you resource usage over time wrt bottlenecks and will serve as a baseline against you can measure the effect of any changes you make. Then start listing / exploring your HW specs, what web server, interpreter, database software your LAMP comprises of, what runs on top of your web server interpreter-wise (Perl, PHP, Python, Java, Ruby, etc), software (web forum, shopping cart, web log, photo gallery, statistics, homebrewn software) including any plugins, then check each software packages version against what the vendor marks as outdated / supported + current, check your web server, interpreter, database configuration for unwanted or unnecessary items, their log files for clues and tell us when you first noticed what you think is a memory leak and what measures you already took.

Valery Reznic 01-20-2013 04:11 PM

Quote:

Originally Posted by ps1x0 (Post 4873567)
I need a script, that executes a command if memory usage on the machine is over 60%. I have a problem with apache and there's a memory leak which I can't locate. And that's the second question. How could I locate which process is causing the leak?
At least twice a day memory usage goes up to 100% until I dont reload or restart the httpd.
The script I need is not a real solution, but it should work.. :)

There is MaxRequestsPerChild configuration option for Apache.
If you limit requests number for each worker process, then you'll limit your memory leak.
After process exited all resources - leaked included - will be freed


All times are GMT -5. The time now is 11:52 PM.