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

#!/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

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.

awk '{print ($1>10? 1 : 0)}' /proc/loadavg

The return of awk can be use to follow with a flow control branch

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

$ echo "9.1" | awk '{print ($1 > 10 ? 1 : 0)}'
0
echo "10.1" | awk '{print ($1 > 10 ? 1 : 0)}'
1

Or set an exit code in awk that can be directly used in the shell's if clause:

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.

You could even use ... awk '{exit ($2>10)}' ...

1 Like

load average is now 20 , but still output of this script is OKAY
which should be execute service ....

Maybe your /proc/loadavg does not work?
Using the 2nd last word from the uptime command:

if uptime | tr -d ',' | awk '{exit ($(NF-2)>10)}'
then
...