Run a script if the server load is above a certain value.
Hi all,
I want to setup a cron job to run a bash script which which calculates the server load and if the load is above certain value(say 15) then stores highest cpu eating users list in to a file for that instant. I have a small script which lists users according to cpu usage.. But i dont know how to check the server load and if and only if its above 15. I hope you understand what i meant. Please help me guyz. |
Hello,
There are several ways to go about this. Look into the commands uptime, w, procinfo (if installed) that you can use in a bash script to check for the load average. Tip: Have a look using Google, there's a site that gives you the entire script. If you post what you already have and tell us where it's failing or where you're in doubt we would be able to help you better. Kind regards, Eric |
Define "server load".
Presuming you mean loadavg, what makes you believe it is related to CPU consumption of individual processes ?. This is a general question, not directed specifically at you. I suspect all the scripts @EricTRA found are also inappropriate in Linux. Just a guess of course ... |
Quote:
I made one of the easiest errors a human can make :cry: I assumed the OP was referring to the Load Average obtained by the commands I referred to. @syg00: What do you mean by 'inappropriate in Linux'? Can you clarify that a bit please? Kind regards, Eric |
Loadavg in linux comprises tasks in the runqueue plus tasks in uninterruptible sleep (state "D"). Usually this is taken to mean tasks with non-completed I/O, but isn't exclusively this.
High count of state "D" tasks will elevate the loadavg without any relationship to CPU consumption. Things like apache have been known to do this. |
Quote:
OK, point taken, thanks for the clarification. Kind regards, Eric |
Hello,
Assuming you are talking about the Load Average, returned by uptime .. Well, I'm not a super shell scripting geek, but I've made a try and written a little script for you, that may help :) Code:
#!/bin/bash Please, let me know if there is something unclear, or if you need some extra help with that.. I'd like to mention that the code above was tested on CentOS 5, Slackware 13. I assume, it should run OK, on other distros. Greetings :) |
Like we say in my country: "there is more than one way to skin a cat"
Code:
#!/bin/bash Execute it with ./insert_script_name_here and it will check cpu load average every 60 secs. Please note that it will execute in the background until you stop it with "killall insert_script_name_here" |
Simply set a cronjob to run at N time, ( where N is any arbitrary limit you want to check ).
In your script make sure you have a condition that checks if the load meets your criteria - if it does not, simply have the script "die" until the next time it's invoked. * Set cron job to run every minute * Quote:
|
Hi..
Thank you all for your quick response. I will try out your suggestions and will updates the thread after. Thanx again. |
Hi all...
I modified WhisperiN suggestion and got what i want. I will try others too. Thank you all nad what i wanted is starting and guyz perfevtly gave that. Love you all &LQ |
Hello,
Glad you've found what you were looking for. If you consider your problem/question solved, then please mark it as such using the Thread Tools. Kind regards, Eric |
Hi,
I am sorry. I did forget that. Thanx for reminding me.. |
All times are GMT -5. The time now is 03:39 PM. |