LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-03-2011, 05:09 AM   #1
waha87
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Rep: Reputation: 0
Question 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.
 
Old 01-03-2011, 05:16 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
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
 
Old 01-03-2011, 05:24 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,359

Rep: Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180
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 ...
 
Old 01-03-2011, 05:31 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Quote:
Originally Posted by syg00 View Post
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
 
Old 01-03-2011, 05:49 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,359

Rep: Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180Reputation: 4180
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.
Old 01-03-2011, 05:51 AM   #6
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Quote:
Originally Posted by syg00 View Post
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
 
Old 01-03-2011, 06:13 AM   #7
WhisperiN
Member
 
Registered: Jun 2009
Location: Middle East
Distribution: Slackware 13.1, CentOS 5.5
Posts: 137

Rep: Reputation: 17
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.
 
Old 01-03-2011, 12:19 PM   #8
Slax-Dude
Member
 
Registered: Mar 2006
Location: Valadares, V.N.Gaia, Portugal
Distribution: Slackware
Posts: 532

Rep: Reputation: 273Reputation: 273Reputation: 273
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.
Old 01-03-2011, 12:33 PM   #9
misconfiguration
Member
 
Registered: Apr 2007
Location: Indianapolis, Indiana
Distribution: RHEL, Fedora, AIX, HP-UX, FreeBSD, Slackware
Posts: 62

Rep: Reputation: 19
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

( ... )
 
Old 01-03-2011, 08:40 PM   #10
waha87
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Original Poster
Rep: Reputation: 0
Hi..

Thank you all for your quick response. I will try out your suggestions and will updates the thread after.

Thanx again.
 
Old 01-10-2011, 09:22 PM   #11
waha87
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Original Poster
Rep: Reputation: 0
Wink

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
 
Old 01-11-2011, 12:55 AM   #12
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
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
 
Old 01-11-2011, 03:27 AM   #13
waha87
LQ Newbie
 
Registered: Oct 2010
Posts: 10

Original Poster
Rep: Reputation: 0
Smile

Hi,

I am sorry. I did forget that.
Thanx for reminding me..
 
  


Reply

Tags
cpu, load average, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Execute script on local server as normal user to run commands on remote server ALInux Linux - Software 1 01-01-2010 06:30 AM
Tools or script to monitor load average on server nazs Linux - General 4 10-27-2009 09:02 PM
I want to run script on the server from client machine in windows in a perl script vpradeep Linux - Newbie 2 09-01-2008 03:29 AM
Need guidance on creating minimal boot CD to load a few program and run a script angotia Linux - General 2 07-10-2008 03:59 AM
server load script cgi newuser455 Programming 1 06-22-2005 07:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration