Portal Home > Knowledgebase > Articles Database > Script to restart services, if they are down.
Script to restart services, if they are down.
Posted by sabarishks, 10-30-2008, 11:29 AM |
Script to restart services, if they are down.
Hello Gurus,
I am desperately looking for all your helps. This is a most important thing.
I m in need of script which monitor services like apache,mysql,ftp etc and if its down it should restart and send an alert mail.
I am not great in bash script. But, i found the following. It works for a single service.
I just want a combination of this for many service. Some one, please help me.
Very big please.
#!/bin/bash
RESTART="/etc/init.d/apache2 restart"
#path to pgrep command
PGREP="/usr/bin/pgrep"
# Httpd daemon name,
# Under RHEL/CentOS/Fedora it is httpd
# Under Debian 4.x it is apache2
HTTPD="httpd"
# find httpd pid
$PGREP ${HTTPD}
if [ $? -ne 0 ] # if apache not running
then
# restart apache
$RESTART
fi
#!/bin/bash
if [ "$(ps aux | grep httpd | wc -l)" -lt "2" ]; then
echo "Mysql is Down" >
|
Posted by Panopta, 10-30-2008, 11:46 AM |
Check out Monit at http://mmonit.com/monit/ - it should be able to handle what you need. There are lots of other tools, such as daemontools, that do similar things, but Monit is pretty straightforward to get setup.
__________________Panopta | Advanced Server Monitoring and Outage Management Solutions
Don't just track downtime. Do something about it.http://www.panopta.com | sales@panopta.com
|
Posted by eger, 10-30-2008, 12:02 PM |
You might also check out SIM at http://rfxnetworks.com/sim.php. It can test some common services if they are up and restart the daemons if needed. Also does load checking and alerting.
|
Posted by sabarishks, 10-30-2008, 12:11 PM |
A big thanks to Eger and Panopta. I will surely try this and let you know.
I was thinking of writing a bash script. One more doubt, Will this works on SUSE linux platform ?
|
Posted by Panopta, 10-30-2008, 12:16 PM |
Glad to help, both SIM and Monit should run fine on SUSE. You might have to customize some of the check scripts to the certain configuration of SUSE, but that shouldn't be too difficult.
Either of these would definitely be easier than starting from scratch with your own script, as you'll start to find more special cases and pieces of functionality that you need to add in, and your script quickly balloons in complexity.
Good luck!
__________________Panopta | Advanced Server Monitoring and Outage Management Solutions
Don't just track downtime. Do something about it.http://www.panopta.com | sales@panopta.com
|
Posted by senne, 10-31-2008, 01:37 AM |
I have been using djb's daemontools,it's very mint
__________________www.pc15w.com
What a 15w pc can do?
|
Posted by jphilipson, 10-31-2008, 02:05 AM |
Another thumbs up for monit... great app, even has an available web based monitor
__________________I perform System Administration
|
Posted by blessen, 10-31-2008, 02:43 AM |
Quote:
Originally Posted by sabarishks
Hello Gurus,
I am desperately looking for all your helps. This is a most important thing.
I m in need of script which monitor services like apache,mysql,ftp etc and if its down it should restart and send an alert mail.
I am not great in bash script. But, i found the following. It works for a single service.
I just want a combination of this for many service. Some one, please help me.
Very big please.
#!/bin/bash
RESTART="/etc/init.d/apache2 restart"
#path to pgrep command
PGREP="/usr/bin/pgrep"
# Httpd daemon name,
# Under RHEL/CentOS/Fedora it is httpd
# Under Debian 4.x it is apache2
HTTPD="httpd"
# find httpd pid
$PGREP ${HTTPD}
if [ $? -ne 0 ] # if apache not running
then
# restart apache
$RESTART
fi
#!/bin/bash
if [ "$(ps aux | grep httpd | wc -l)" -lt "2" ]; then
echo "Mysql is Down" >
|
Posted by barry[CoffeeSprout], 10-31-2008, 06:23 AM |
I've used SIM and Webmin before... Monit looks very nice and I'll have to give it a spin
|
Posted by whmcsguru, 10-31-2008, 02:59 PM |
Never, ever, ever use something to "automatically restart services", or "automatically restart the server". That's the QUICKEST way to get yourself DOS'ed to death.
A script such as SIM will easily allow individuals to flood your server with requests, DOS the server, cause it to reboot constantly, etc.
Notifications of port unavailability? Good. Automatic responses (ie: restart this service)? Bad.
__________________Linux Tech Networks: Reliable Server Administration and Monitoring since 2002VBulletin Development: VBulletin customizations, development, hosting and leased licenses
|
Posted by barry[CoffeeSprout], 11-01-2008, 09:51 AM |
Hi,
I agree that a server should never reboot itself (because trouble with the OS could mean it never comes back up)
However I do think it's good that a script automatically restarts broken services.
In a DOS situation the service will likely fall down again, but until human hands and eyes arrive it does provide the following:
-More logs on what is happening.. more is always better
-keeps providing some level of service depending on how bad things are.
A script should however have an easy kill button so that it can be disabled as soon as an admin logs in (which should generally be pretty shortly after the notifications fly)
|
Posted by sabarishks, 11-05-2008, 11:54 AM |
Thanks a lot for you all Guys. Sim is excellent, But need to consider about the security holes.
However, Finally I managed to write a script of my Own and it works like a charm. I am happy now. Pasting it below. So that others can see it.
MAILTO="youname@yourdomain.com"
DATE= echo `date`
function mysql(){
if [ "$(ps aux | grep mysql | wc -l)" -lt "2" ]; then
echo "Mysql is Down" > then
echo "Httpd is Down" > then
echo "Tomcat is Down" >
|
Add to Favourites Print this Article
Also Read