Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Apache keep dying on you? Apache watchdog script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
ben-xo
n00b
n00b


Joined: 13 Dec 2004
Posts: 38

PostPosted: Tue Apr 25, 2006 2:10 pm    Post subject: Apache keep dying on you? Apache watchdog script Reply with quote

http://www.ben-xo.com/v6/2006/04/25/apache-watchdog/

I got a bit bored of my web server switching itself off at 4am for no reason and the boss waking me up from half way across the world all irate about it. So I wrote this script so that the problem can fix itself.

Feel free to use it for whatever you want, although I can’t promise it’ll help make tea or clean shoes.

watchdog.php
Code:
<?php

### COPYRIGHT (c)2006 Ben Isaacs (XO)

### APPLICATION: Watchdog
###     VERSION: 0.2
###     HISTORY: 2006-04-08 Initial Version
###         0.2: 2006-04-25 Made it more portable.

/************************************************************************
This program is freeware, and may be modified or redistributed in any way.
It comes with ABSOLUTELY NO WARRANTY of any kind, express or implied,
including the assumption of fitness for any purpose. Use at your own risk.

Author: Ben Isaacs http://www.ben-xo.com/

Usage:
1) modify the config below for your web server
2) add a line to crontab something like

   * * * * * (cd /where/ever/; php watchdog.php)

3) touch /tmp/.xo.watchdog

  (otherwise the first time it runs, it'll think apache's crashed and
   restart it nastily.)

************************************************************************/

/** config **/

define('WATCHED_WEBSITE', 'http://www.ben-xo.com/');
define('NOTIFICATION_EMAIL', 'watchdog@ben-xo.com');

// these are for Gentoo
$restart_commands = array(
   'killall -9 apache2',
   '/etc/init.d/apache2 zap',
   '/etc/init.d/apache2 start'
);

/***********************************************************************/

if(!file_exists('/tmp/.xo.watchdog')) {
   // previous watchdog must have timed out
   watchdog_restart();
   touch('/tmp/.xo.watchdog');
   exit();
} else {
   unlink("/tmp/.xo.watchdog");
}

// if the connection below hangs, then the script will time out after 15 seconds
// leaving the flag file deleted. next time this script is run, it will detect
// the time-out, using the block above.

set_time_limit(15); // seconds
$page = file(WATCHED_WEBSITE);

if(empty($page)) {
   watchdog_restart();
}
touch('/tmp/.xo.watchdog');

function watchdog_restart() {
   global $restart_commands;

   foreach ($restart_commands as $c) {
      system($c);
   }

   mail(NOTIFICATION_EMAIL, 'Watchdog @ '.date('r'), 'restarted apache watching '.WATCHED_WEBSITE);
}

?>

_________________
Ben XO
Back to top
View user's profile Send private message
ben-xo
n00b
n00b


Joined: 13 Dec 2004
Posts: 38

PostPosted: Tue Apr 25, 2006 2:12 pm    Post subject: Reply with quote

(I would just like to use this opportunity to express how badly layed out I think the Gentoo forums are. So many subforums, it's impossible to find anything useful unless you know them all inside out. And it's bad form to cross-post - yet, I can't tell which forum this thread would be most appropriate in. [/rant]).
_________________
Ben XO
Back to top
View user's profile Send private message
Maedhros
Bodhisattva
Bodhisattva


Joined: 14 Apr 2004
Posts: 5511
Location: Durham, UK

PostPosted: Tue Apr 25, 2006 4:15 pm    Post subject: Reply with quote

ben-xo wrote:
I can't tell which forum this thread would be most appropriate in.

It's not a support request, so DT&T is probably the best place for it.

Moved from Networking & Security to Documentation, Tips & Tricks.
_________________
No-one's more important than the earthworm.
Back to top
View user's profile Send private message
ben-xo
n00b
n00b


Joined: 13 Dec 2004
Posts: 38

PostPosted: Tue Apr 25, 2006 5:05 pm    Post subject: Reply with quote

Maedhros wrote:
ben-xo wrote:
I can't tell which forum this thread would be most appropriate in.

It's not a support request, so DT&T is probably the best place for it.

Moved from Networking & Security to Documentation, Tips & Tricks.


Thankyou, although I wonder if it might also be appropriate in Unsupported Software.

IMO the forum categories are too narrowly defined on this site. Many threads fall into several categories (especially once people who don't care about categorisation start posting in them.)

But, that's a topic for another thread... (grin)
_________________
Ben XO
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum