Forum Moderators: bakedjake

Message Too Old, No Replies

wanted a unix script

to restart httpd

         

indiandomain

6:38 pm on May 6, 2003 (gmt 0)

10+ Year Member



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.

drbrain

6:42 pm on May 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



getty can do it (is that a FreeBSD-ism?) (no, I'm really serious here)

You need to run it in the foreground, not as a daemon, as a fork'd process of your script and watch for an error exit code, then just re-fork. Be careful not to restart too quickly, or you may kill your system.

wruk999

6:45 pm on May 6, 2003 (gmt 0)

10+ Year Member



Hi indiandomain,

Can you not just put your command: /etc/init.d/httpd restart
into a .sh script. put it in a folder somewhere on your server, and call it from cron however often you want to.

I have a few shell scripts which do all sorts on cron ;) restarting a few of the daemons included.

<added>Running it from cron *should* run it as root, which is what you need, though you will have to double-cjeck that it does run from root</added>

wruk999

indiandomain

6:54 pm on May 6, 2003 (gmt 0)

10+ Year Member



guys i tried working on the cron job.
but couldnt get things working..
thats when i posted my request here.

wruk999-im trying your .sh advice...but i need something that detects httpd failure and then runs the script.

would a cron job mess the server if i keep restarting httpd every hour?

rayvd

7:07 pm on May 6, 2003 (gmt 0)

10+ Year Member



Create the following script:


#!/bin/sh
/etc/init.d/httpd restart

Call it /root/bin/webcheck.sh (just an example). Then add it to your crontab and run it as often as you like... if you want to run it once an hour, do the following:


# crontab -e

Now, here's what your cron entry should look like:


0 * * * * /root/bin/webcheck.sh &>/dev/null

That should do the trick! It will run once at the top of every hour.

wruk999

7:08 pm on May 6, 2003 (gmt 0)

10+ Year Member



it *shouldn't* affect it .. but I could be wrong..I do restart mine regularly..but I'm sure a unix admin can correct me if I'm wrong. ;)

sugarkane

5:42 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> it *shouldn't* affect it

True, but I was once caught out by a script that restarted the server in the dead of night - only for some reason, the restart failed, leaving the server down all night :(

I'd feel safer using something like this script on a cron, which should detect if httpd has stopped and restart it if so:

#!/usr/bin/perl
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$ua->agent("Server Status Check");
$req = new HTTP::Request 'GET' => 'http://127.0.0.1';
$req->header('Accept' => 'text/html');
$res = $ua->request($req);
if ($res->is_success) {
# Things seem fine!
} else {
$foo=`/etc/init.d/httpd restart`;
}

indiandomain

5:42 am on May 13, 2003 (gmt 0)

10+ Year Member



sugarkane

should i replace 127.0.0.1 with my server id?
i tried that and it doesnt work.

i used chmod755

sugarkane

3:51 pm on May 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should change 'http://127.0.0.1' to a valid page on your own server - 127.0.0.1 should bring up the index page of your default virtual host, but you could just as easily use your own domain name.

It just has to reach a valid page - in this simple script anything like a 404 error will be treated as a failure and cause a server restart.