
10-30-2008, 11:29 AM
|
|
Junior Guru Wannabe
|
|
Join Date: Sep 2008
Location: Bangalore
Posts: 77
|
|
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" >
|

10-30-2008, 11:46 AM
|
|
Web Hosting Guru
|
|
Join Date: May 2007
Location: Chicago
Posts: 322
|
|
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
|

10-30-2008, 12:02 PM
|
|
Web Hosting Evangelist
|
|
Join Date: Feb 2004
Location: Bay Area, CA
Posts: 521
|
|
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.
|

10-30-2008, 12:11 PM
|
|
Junior Guru Wannabe
|
|
Join Date: Sep 2008
Location: Bangalore
Posts: 77
|
|
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 ?
|

10-30-2008, 12:16 PM
|
|
Web Hosting Guru
|
|
Join Date: May 2007
Location: Chicago
Posts: 322
|
|
Glad to help, both SIM and Monit should run fine on SUSE. You might have to customize some of the check scripts to the particular 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
|

10-31-2008, 01:37 AM
|
|
Junior Guru Wannabe
|
|
Join Date: Feb 2007
Posts: 44
|
|
I have been using djb's daemontools,it's very mint
__________________www.pc15w.com
What a 15w pc can do?
|

10-31-2008, 02:05 AM
|
|
Web Hosting Evangelist
|
|
Join Date: Jan 2003
Posts: 512
|
|
Another thumbs up for monit... great app, even has an available web based monitor
__________________I perform System Administration
|

10-31-2008, 02:43 AM
|
|
Web Hosting Master
|
|
Join Date: Aug 2003
Location: Gods Own Country
Posts: 850
|
|
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" >
|

10-31-2008, 06:23 AM
|
|
Web Hosting Master
|
|
Join Date: Oct 2003
Location: The Netherlands
Posts: 1,187
|
|
I've used SIM and Webmin before... Monit looks very nice and I'll have to give it a spin
|

10-31-2008, 02:59 PM
|
|
<?require_once("life")?>
|
|
Join Date: Sep 2002
Location: inside your network
Posts: 9,548
|
|
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
|

11-01-2008, 09:51 AM
|
|
Web Hosting Master
|
|
Join Date: Oct 2003
Location: The Netherlands
Posts: 1,187
|
|
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)
|

11-05-2008, 11:54 AM
|
|
Junior Guru Wannabe
|
|
Join Date: Sep 2008
Location: Bangalore
Posts: 77
|
|
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" >
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
| Postbit Selector |
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
| Login: |
|
|
| Advertisement: |
|
|
| Web Hosting News: |
|
|
|