Check load and action


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check load and action
# 1  
Old 11-04-2014
Check load and action

Hey everyone
can you check this script logic ?
it has to restart webservice if found server load is higher than X,
also i have put it in crontab to run every one minute


Code:
#!/bin/bash
loadavg=$(uptime | awk -F "." '{ print $1 }' | awk -F ":" '{ print $5 }')
if [ "$loadavg" -ge "10" ]; then
pkill -9 httpd
service httpd restart
sleep 5
service varnish restart
loadavg=0
fi

# 2  
Old 11-04-2014
Some comment:
I would stay away from that SIGNAL -9, at least I would find out what could be the side-effects of it.

If httpd is not running because you kill it, service httpd start should be the more appropriated command. Speaking of appropriated, service httpd stop would be a better choice than pkill -9 httpd

It is best to use absolute paths in a cronjob. e.g. /sbin/service httpd start

Interesting you are using a variable named loadavg. The Linux kernel keeps a realtime record of that average load in /proc/loadavg. Reading from that would be cleaner that using uptime.

Let's use awk to handle the floating point as well.
Code:
awk '{print ($1>10? 1 : 0)}' /proc/loadavg

The return of awk can be use to follow with a flow control branch
# 3  
Old 11-04-2014
the output of awk is always 1
awk '{print ($1>10? 1 : 0)}' /proc/loadavg
but loadavg is useful in this case at it show the right load,
can you check the result of your line? coz i get 1 in all time
# 4  
Old 11-04-2014
Code:
$ echo "9.1" | awk '{print ($1 > 10 ? 1 : 0)}'
0
echo "10.1" | awk '{print ($1 > 10 ? 1 : 0)}'
1

# 5  
Old 11-04-2014
Quote:
Originally Posted by Aia
Some comment:
I would stay away from that SIGNAL -9, at least I would find out what could be the side-effects of it.

If httpd is not running because you kill it, service httpd start should be the more appropriated command. Speaking of appropriated, service httpd stop would be a better choice than pkill -9 httpd

It is best to use absolute paths in a cronjob. e.g. /sbin/service httpd start

Interesting you are using a variable named loadavg. The Linux kernel keeps a realtime record of that average load in /proc/loadavg. Reading from that would be cleaner that using uptime.

Let's use awk to handle the floating point as well.
Code:
awk '{print ($1>10? 1 : 0)}' /proc/loadavg

The return of awk can be use to follow with a flow control branch
Or set an exit code in awk that can be directly used in the shell's if clause:
Code:
if awk '{exit ($2>10? 1 : 0)}' /proc/loadavg
then
  : echo okay
else
  service httpd stop
  sleep 1
  kill -9 -x httpd # hard-kill remaining httpd's
  service httpd start
  sleep 5
  service varnish restart
fi

I use the 5 minutes average ($2=2nd value), and this should be run every 5..10 minutes.

Last edited by MadeInGermany; 11-04-2014 at 03:07 PM..
# 6  
Old 11-04-2014
You could even use ... awk '{exit ($2>10)}' ...
This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-05-2014
Quote:
Originally Posted by MadeInGermany
Or set an exit code in awk that can be directly used in the shell's if clause:
Code:
if awk '{exit ($2>10? 1 : 0)}' /proc/loadavg
then
  : echo okay
else
  service httpd stop
  sleep 1
  kill -9 -x httpd # hard-kill remaining httpd's
  service httpd start
  sleep 5
  service varnish restart
fi

I use the 5 minutes average ($2=2nd value), and this should be run every 5..10 minutes.
load average is now 20 , but still output of this script is OKAY
which should be execute service ....
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script fro file check and load the trail file

Hi, Im going to use shell script for load the data into DB. First i need to read the trail file(csv file has two columns with comma separated ) like file name trail1024(last 4 digitsMMDD). In this trail file 27 entries will have like below,I need to read first csv file name and get the 4... (1 Reply)
Discussion started by: krajasekhar.v
1 Replies

2. UNIX for Dummies Questions & Answers

VPS has load 200, httpd load no activity, netstat nothing

Hello, on my hostserver i see one VPS of mine got load of 200.00 and netstat nothing (not a single blank line on netstat command) after some time, netstat started showing connections, but i see no excessive IP connections. tail -f /var/log/httpd/access_log shows no activity /var/log/messages ;... (1 Reply)
Discussion started by: postcd
1 Replies

3. Shell Programming and Scripting

Check date and do action

I would like to write a script to do such thing , if a directory have file that the extension is today (yymmdd), then do a specific action , I will run a cron job to run this script . For example Today is 2nd , if the directory have files which file name are abc.121102 and def.121102 ,... (6 Replies)
Discussion started by: ust3
6 Replies

4. Shell Programming and Scripting

Check value of load balance dynamically

Hi All, I want to check load balance(or CPU utilzation) on server dynamically based on that value i want to perform some operation . Means if load balance is <10 then start server . Am using prstat -a 1 commadn it is listing CPU utilzation/load balancing dynamically .. How to take this... (7 Replies)
Discussion started by: vivek1489
7 Replies

5. UNIX for Advanced & Expert Users

Autosys: Check the jil load time

Hi All, I wanted to know what time the box jobs were loaded into a particular Autosys Instance. Is there a autosys command to find out the above? (The timestamp at which a box was loaded) I had loaded my JIL script without outputting it to the log file. (4 Replies)
Discussion started by: grep_me
4 Replies

6. Shell Programming and Scripting

same action in then as in else: explanation?

in /etc/init.d/networking of an ubuntu computer, I found this code: if ifdown -a --exclude=lo; then log_action_end_msg $? else log_action_end_msg $? fi Shouldn't it be replace by ifdown -a --exclude=lo ... (0 Replies)
Discussion started by: raphinou
0 Replies

7. Shell Programming and Scripting

Need help in wrting Load Script for a Load-Resume type of load.

hi all need your help. I am wrting a script that will load data into the table. then on another load will append the data into the existing table. Regards Ankit (1 Reply)
Discussion started by: ankitgupta
1 Replies

8. Shell Programming and Scripting

action command

Hi.. When i refered the script /etc/rc.sysinit... i found the "action commands" like But this is not working in my shells.. the following error is coming... Please anybody help Thanks in advance esham (5 Replies)
Discussion started by: esham
5 Replies
Login or Register to Ask a Question