How to Kill/Restart on High Load?

You can check to see if apache is being restarted; you can look in one of the apache logs for that.

Or you can rewrite the script to send you an email.

Jeff
 
Search and install this script:

# +----------------------------------------------------------------------------
# | chkApache.sh
# | Written for www.radixhosting.com
# |
# | - Performs a basic scan to check the Apache load
# | - Forcefully restarts Apache if necessary
# | - Sends a report to the system administrator if actions taken
# +----------------------------------------------------------------------------


Modify it to where the httpd-status is located at.


`lynx -dump http://127.0.0.1/httpd-status`


-Alon
 
Found the script here:

http://forums.hostsearch.com/showthread.php?t=3663

Running OK from the command line. Should I use a cronjob to run it every 5 minutes?

My OS is Fedora 4 and I am running apache 2.0


No!

The last line in the code does this for you:

sleep $DELAY


The default is "check every 3 seconds".

You need to log in as root and execute the following command:

/bin/bash /root/chkApache.sh > /dev/null 2>&1 &


Then, you need to exit from the root and log back in.
Once you log back in,. run:

ps fax

and you will see that the process is running as a job continiously.
It is self-running,. no need to cron job it.
 
Great!

Now how to kill this script's process without rebooting the server? I need to edit the cpu limit and then rerun the script.

Also, how can I modify this line:

ADMIN_MAIL=root

to let it email report to an email address not hosted by this same server.
 
Last edited:
Great!

Now how to kill this script's process without rebooting the server? I need to edit the cpu limit and then rerun the script.

Also, how can I modify this line:



to let it email report to an email address not hosted by this same server.


kill -9 <processid>

Yes,. modify the email address, simply put an address instead of the word root.
Also, you can change the value of whatever you like it it.
 
The high load of my server is mainly caused by MySQL - still looking for which database has been hacked. When I manually stop and restart MySQL from DA control panel, everything runs smoothly. How to modify the script to imitate this manual process?
 
Search these forums for a reference to mytop to figure out which database is causing your overload.

Jeff
 
You are right, Jeff!

smtalk helped me to get mytop work and indeed, just a few minutes of running mytop as root, I caught it!

It turned out to be a database for a guestbook script that the site owner installed and then abandoned using it. That was an advanced guestbook script that used a MySQL database to store the guestbook entries. However, the owner of the website did not remove the script and the database that was associated with it. This out-of-date script left the door open for hacking.

I removed both the script and its MySQL databse and now the server is stable ever after :-)
 
Or until the next hack.

I remember when I thought hosting was an easy career ;) .

Jeff
 
No!

The last line in the code does this for you:

sleep $DELAY


The default is "check every 3 seconds".

You need to log in as root and execute the following command:

/bin/bash /root/chkApache.sh > /dev/null 2>&1 &


Then, you need to exit from the root and log back in.
Once you log back in,. run:

ps fax

and you will see that the process is running as a job continiously.
It is self-running,. no need to cron job it.


Nice script, but it seems to work only as long as I'm logged in as root. Each time I log out the process the script runs is killed so the script isn't monitoring anymore. Is there a solution for this? I'd like to run the script also when I'm not logged in.
 
I am using the script and it is running with a cronjob - I can see it is running from /var/log/cron. But how do I know if the script is working in restarting apache when the load exceeds the limit that is set? What log should I check or create?

My OS is Fedora 4 and the apache version is 2.0 - I built apache using customapache.

Hi.

It is probably not practical to even use this script as it is gonna just kill your running httpd server so it goes completely offline. I only was trying to provide this person with a practical way to accomplish what he wanted to do. But to provide logging method you can use this method.

Code:
#!/bin/sh

max_cpu="10" # Set to max cpu. This is the max % you will allow no decimals.
apache_name="httpd" # Set to the name that apache runs as. Usually apache but sometimes it may be httpd.
log_file=/var/log/messages # Log File path.

################## DONT TOUCH ##################
time=$(date "+%y:%m %p on %D")
date=$(date "+%h %d %T")
cpu_usage_apache=$(ps xua | grep -iw $apache_name | grep -v grep | awk '{print $3}' | sed -e 's/..$//' | awk '!L[$0]++' | sort -nr)

if [ -z "$cpu_usage_apache" ]; then
        echo "not running"
        exit 1
fi

for i in $cpu_usage_apache; do

        if [ "$i" -gt "$max_cpu" ]; then
                ps xua | grep -iw $apache_name | grep -v grep | awk '{print $2}' | xargs kill
                echo "$date httpd reset: Httpd server shutdown at $time by script." >> $log_file
                exit 0

        else
                echo >/dev/null
        fi

done

exit 0
 
Last edited:
Nice script, but it seems to work only as long as I'm logged in as root. Each time I log out the process the script runs is killed so the script isn't monitoring anymore. Is there a solution for this? I'd like to run the script also when I'm not logged in.
Add it to your inittab

Use the runonce feature... as it loops.
 
Error

Hai

I tried the script but I get an error on :

./httpd-tohigh.sh: line 68: syntax error near unexpected token `<<<'
./httpd-tohigh.sh: line 68: ` mail -s "chkApache.sh alert on `hostname`" $ADMIN_MAIL <<< cat <<EOF'

Any hints

GNU bash, version 2.05a.0(1)-release (i386-slackware-linux-gnu)
Copyright 2001 Free Software Foundation, Inc.

Chrs J.P..
 
Hai

I tried the script but I get an error on :

./httpd-tohigh.sh: line 68: syntax error near unexpected token `<<<'
./httpd-tohigh.sh: line 68: ` mail -s "chkApache.sh alert on `hostname`" $ADMIN_MAIL <<< cat <<EOF'

Any hints

GNU bash, version 2.05a.0(1)-release (i386-slackware-linux-gnu)
Copyright 2001 Free Software Foundation, Inc.

Chrs J.P..

Remove one < from the set of three <<< then try the script again.

Also may need a space between them like this "< <<"
 
Last edited:
If you server are overloaded by MySQL with slow "select" queries that take a long time to execute, you can kill those specifics child processes using this script.

It kills all MySQL child processes over 30 seconds...
(You can put this script in crontab for auto run every 30" or 1')

Code:
for pid in `mysqladmin -uroot processlist | grep -i select | awk -F"|" '$7>=30{print $2}'`; do { mysqladmin kill $pid; echo "Pid $pid Killed"; } ; done

You can read more about long time queries at these links:
http://dev.mysql.com/doc/refman/5.0/en/slow-query-log.html
http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#option_mysqld_long_query_time

I Hope This Helps!
 
Last edited:
Back
Top