Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-03-2011, 05:09 AM
|
#1
|
LQ Newbie
Registered: Oct 2010
Posts: 10
Rep:
|
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.
|
|
|
01-03-2011, 05:16 AM
|
#2
|
LQ Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
|
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
|
|
|
01-03-2011, 05:24 AM
|
#3
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,359
|
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 ...
|
|
|
01-03-2011, 05:31 AM
|
#4
|
LQ Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
|
Quote:
Originally Posted by syg00
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 ...
|
Hi,
I made one of the easiest errors a human can make  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
|
|
|
01-03-2011, 05:49 AM
|
#5
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,359
|
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.
Last edited by syg00; 01-03-2011 at 05:52 AM.
Reason: d'oh - "s/wait/sleep/"
|
|
1 members found this post helpful.
|
01-03-2011, 05:51 AM
|
#6
|
LQ Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
|
Quote:
Originally Posted by syg00
Loadavg in linux comprises tasks in the runqueue plus tasks in uninterruptible wait (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.
|
Hi,
OK, point taken, thanks for the clarification.
Kind regards,
Eric
|
|
|
01-03-2011, 06:13 AM
|
#7
|
Member
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137
Rep:
|
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
# Set the Load Average below where the script should be fired..!!
WhenLoadAverage=15
which bc
if [[ "$?" -eq "0" ]]; then
bc=`which bc`
fi
load1=`uptime | awk {'print $10'} | cut -d',' -f1`
load2=`uptime | awk {'print $11'} | cut -d',' -f1`
load3=`uptime | awk {'print $12'} | cut -d',' -f1`
echo "Load for 1m = $load1"
echo "Load for 5m = $load2"
echo "Load for 15m = $load3"
load1=`echo $load1*100 | $bc`
load2=`echo $load2*100 | $bc`
load3=`echo $load3*100 | $bc`
load1=`echo $load1 | cut -d'.' -f1`
load2=`echo $load2 | cut -d'.' -f1`
load3=`echo $load3 | cut -d'.' -f1`
echo "Load where I should take action is: $WhenLoadAverage"
WhenLoadAverage=`echo $WhenLoadAverage*100 | $bc`
WhenLoadAverage=`echo $WhenLoadAverage | cut -d'.' -f1`
# Change load1 below, to the load you want to keep eye on..
# load1 = 1 min load.
# load2 = 5 min load.
# load3 = 15 min load.
if [[ "$load1" -ge "$WhenLoadAverage" ]]; then
# Put your script here, or the script you want to run
echo 'Script is fired'
else
echo 'All is Cool..!!'
fi
exit
# DONE
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 
Last edited by WhisperiN; 01-03-2011 at 06:17 AM.
|
|
|
01-03-2011, 12:19 PM
|
#8
|
Member
Registered: Mar 2006
Location: Valadares, V.N.Gaia, Portugal
Distribution: Slackware
Posts: 532
|
Like we say in my country: "there is more than one way to skin a cat"
Code:
#!/bin/bash
EXECUTE_ON_AVERAGE="15" # if cpu load average for last 60 secs is
# greater or equal to this value, execute script
# change it to whatever you want :-)
while true; do
if [ $(echo "$(uptime | cut -d " " -f 13 | cut -d "," -f 1) >= $EXECUTE_ON_AVERAGE" | bc) = 1 ]; then
echo "execute the script"
else
echo "do nothing"
fi
sleep 60
done
Copy the code to a file named insert_script_name_here and "chmod 755" it.
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"
Last edited by Slax-Dude; 01-03-2011 at 12:22 PM.
|
|
1 members found this post helpful.
|
01-03-2011, 12:33 PM
|
#9
|
Member
Registered: Apr 2007
Location: Indianapolis, Indiana
Distribution: RHEL, Fedora, AIX, HP-UX, FreeBSD, Slackware
Posts: 62
Rep:
|
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:
#!/usr/bin/perl -w
die unless( $server_load eq N );
# If the server is equal to or above N
( ... )
|
|
|
|
01-03-2011, 08:40 PM
|
#10
|
LQ Newbie
Registered: Oct 2010
Posts: 10
Original Poster
Rep:
|
Hi..
Thank you all for your quick response. I will try out your suggestions and will updates the thread after.
Thanx again.
|
|
|
01-10-2011, 09:22 PM
|
#11
|
LQ Newbie
Registered: Oct 2010
Posts: 10
Original Poster
Rep:
|
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
|
|
|
01-11-2011, 12:55 AM
|
#12
|
LQ Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
|
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
|
|
|
01-11-2011, 03:27 AM
|
#13
|
LQ Newbie
Registered: Oct 2010
Posts: 10
Original Poster
Rep:
|
Hi,
I am sorry. I did forget that.
Thanx for reminding me..
|
|
|
All times are GMT -5. The time now is 04:01 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|