Desperados
Verified User
- Joined
- Jan 2, 2017
- Messages
- 37
Hello,
i need a Bash script to stop apache when server load is high.
i found this :
#!/bin/bash
loadavg=`uptime | awk '{print $9}'`
if [ "$loadavg" = "average:" ]
then
echo "not this one..."
loadavg=`uptime | awk '{print $10}'`
fi
RESTART="/sbin/service httpd restart"
STOP="/sbin/service httpd stop"
# bash doesn't understand floating point
# so convert the number to an interger
thisloadavg=`echo $loadavg|awk -F \. '{print $1}'`
if [ "$thisloadavg" -ge "10" ]; then
echo "Busy - Load Average $loadavg ($thisloadavg) "
#httpd -k stop
STOP
elif [ "$thisloadavg" -le "1" ]; then
echo "Okay - Load Average $loadavg ($thisloadavg) "
pgrep httpd
if [ $? -ne 0 ] # if apache not running
then
# restart apache
$RESTART
echo "restart!"
else
echo "no restart!"
fi
else
echo "waiting...!"
fi
does it work and is it harmless ?
Best Regards
i need a Bash script to stop apache when server load is high.
i found this :
#!/bin/bash
loadavg=`uptime | awk '{print $9}'`
if [ "$loadavg" = "average:" ]
then
echo "not this one..."
loadavg=`uptime | awk '{print $10}'`
fi
RESTART="/sbin/service httpd restart"
STOP="/sbin/service httpd stop"
# bash doesn't understand floating point
# so convert the number to an interger
thisloadavg=`echo $loadavg|awk -F \. '{print $1}'`
if [ "$thisloadavg" -ge "10" ]; then
echo "Busy - Load Average $loadavg ($thisloadavg) "
#httpd -k stop
STOP
elif [ "$thisloadavg" -le "1" ]; then
echo "Okay - Load Average $loadavg ($thisloadavg) "
pgrep httpd
if [ $? -ne 0 ] # if apache not running
then
# restart apache
$RESTART
echo "restart!"
else
echo "no restart!"
fi
else
echo "waiting...!"
fi
does it work and is it harmless ?
Best Regards