View previous topic :: View next topic |
Author |
Message |
ben-xo n00b

Joined: 13 Dec 2004 Posts: 38
|
Posted: Tue Apr 25, 2006 2:10 pm Post subject: Apache keep dying on you? Apache watchdog script |
|
|
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 cant promise itll 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 |
|
 |
ben-xo n00b

Joined: 13 Dec 2004 Posts: 38
|
Posted: Tue Apr 25, 2006 2:12 pm Post subject: |
|
|
(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 |
|
 |
Maedhros Bodhisattva


Joined: 14 Apr 2004 Posts: 5511 Location: Durham, UK
|
Posted: Tue Apr 25, 2006 4:15 pm Post subject: |
|
|
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 |
|
 |
ben-xo n00b

Joined: 13 Dec 2004 Posts: 38
|
Posted: Tue Apr 25, 2006 5:05 pm Post subject: |
|
|
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 |
|
 |
|
|
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
|
|