Forum Moderators: bakedjake
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
#!/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.
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`;
}
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.