Web Hosting Talk







View Full Version : wanted a unix script-to restart httpd


Digger
05-06-2003, 02:47 PM
guys
i have this server problem.
the httpd crashes once a day on my server.
when it crashed i need to restart httpd using
/etc/init.d/httpd restart

has anyone got a shell script which detects httpd failure and restarts httpd automatically.

any help will be appreciated.

astanley
05-06-2003, 04:09 PM
Digger,
Here is a script that should work - you can run it via a crontab at any time increment, when it detects that there are no httpd processes it will issue the /etc/init.d/httpd restart command.

#!/bin/bash

CNT=`ps ax | grep -v grep | grep -c httpd`
if [ $CNT -le 0 ]
then
echo 'Restarting HTTPD...'
/etc/init.d/httpd restart
fi

BMurtagh
05-06-2003, 05:25 PM
as a side not to adam's post, specificially addressed to adam, do you need to include exit 0 after that if statement. i always thought you needed to include that to like end the script.

hololi
05-07-2003, 06:51 AM
my $0.02

I don't know what version of Uinx/linux you are using but here's some tips

if you installed apache
you usually can tell if it's running by the existence of its "pid" file. The location of the pidfile is in httpd.conf. (usually in the logs directory allong with the apache error log) It is simply a file that contains
the process number of the root apache process.

what I do is the following ...

check for pid file and cross reference with httpd processes (this is better than straight ps command as you can discern between multiple instances of apache running on the same box)

apache comes with a program called apachectl (in the apache bin directory) This can be used to stop, start , restart and check the syntax of the conf file. I wrap this into a script to make it a bit more robust ...


below is a fragment of a script I have for checking the status.. hope it helps needless to say this can be wrapped into a script to restart if its down and cron'd to poll the server. I also try and find out why apache is crashing this is not at all like the reliable apache I know and love


good luck
hololi

# PREAMBLE : This script section determines the status of the webserver
# exit code 0 - pid file exists and the process it records are running ... webserver running ok
# exit code 2 - pid file exists and the process it records is not running ... webserver not runing (unclean shutdown)
# exit code 3 - pid file does not exist but apache processes pointing to /PATH_TO_APACHE/bin/httpd ...
# webserver running but out of control
# exit code 1 - pid file does not exist and no apache processes running ... webserver not running
#
# Parameters : 0 parameters.

#declare env variables
PATH_TO_APACHE= (wherever you have apache installed)
APACHE_CONFIG_PIDFILE=$PATH_TO_APACHE/logs/httpd.pid




if [ -f $APACHE_CONFIG_PIDFILE ]
then
PID_NUM=`cat $APACHE_CONFIG_PIDFILE`
process_exists=` ps -efwww | grep $PID_NUM | grep root | grep $APACHE_CONFIG_HOME/bin/httpd | wc -l`
if [ $process_exists -eq 1 ]
then
echo "webserver running normally"
exit 0
else
echo "webserver processes not running but pid file exists (unclean shutdown ?)"
exit 2
fi
else # no pid file
process_exists=` ps -efwww | grep $APACHE_CONFIG_HOME/bin/httpd | wc -l`
if [ $process_exists -gt 1 ]
then
echo "webserver running but out of control (accidental pid file deletion?)"
exit 3
else
echo "webserver not running (normal state)"
exit 1
fi
fi



this can then be wrapped into another script that is cron'd to poll the webserver i.e if the above fragment was placed in a script called "webserver_status" then you could do ...

#!/bin/bash
# call webserver_status
./webserver_status
WEBSERVER_RESULT=$?

if [ $WEBSERVER_RESULT -ne 0]
then # an exit code of 0 means apache is running fine

#call a script to start up apache
./(whever your apache is)/apacheclt start
fi




# hope this helps if you don't understand the script fragments above let me know



aside : exit codes are not strictly necessary but good coding pratice and vital if you script is called from other scripts where exact error conditions are needed and have to be acted upon. The unix way is an exit code of 0 means sucessful execution of script

$? returns the exit code of a command
type any linux/unix command and immediatly type echo "$?" and you will see the exit code returned

Crocket
04-19-2004, 07:05 PM
* Opps posted on wrong forum (window)! LOL

beowulfdk
04-22-2004, 05:17 AM
Anyone got a script for restarting apache when it crashes but does not end, ie. just sits in memory and refuses to deliver any pages. I'm having this problem. I'm thinking the script should be run by cron and request a html page on the server and examine the server response- and if it refuses connection it should restart apache.

Techark
04-22-2004, 05:37 AM
You guys need SIM http://www.r-fx.org/downloads/sim-current.tar.gz

beowulfdk
04-22-2004, 07:24 PM
Trying SIM out now. Looks very nice!

It has precisely the feature that I was looking for: not just checking that httpd is running, but actually checking if it is locked up (as mine does) by requesting an URL.

Time to see if it restarts httpd next time it crashes, me hopes :)

UH-Matt
04-22-2004, 07:26 PM
I dont know what we would do without SIM, highly recommend it!

beowulfdk
04-23-2004, 04:18 AM
It worked, restarting apache when it locked up (still hanging in memory) this night (on schedule:((( )... Now I can sleep easier at night hehe. Now back to finding out why apache hangs.. :/

Thanks Techark for pointing to that script!
Only problem is that the configure program failed to properly find exim hehe-

NexDog
04-23-2004, 08:15 AM
I would rather investigate to see why apache was hanging up and fix that. Band-aids are great (I really do love them) but should only be used while you are researching the root cause.