hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Hosting Security and Technology : Script to restart services, if they are down.
Reply

Hosting Security and Technology Configuring and optimizing web hosting servers and operating systems, developing administration scripts, building servers, protecting against hackers, and general security (SSL certificates, etc.)
Forum Jump

Script to restart services, if they are down.

Reply Post New Thread In Hosting Security and Technology Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 10-30-2008, 11:29 AM
sabarishks sabarishks is offline
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" >

Reply With Quote


Sponsored Links
  #2  
Old 10-30-2008, 11:46 AM
Panopta Panopta is offline
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

Reply With Quote
  #3  
Old 10-30-2008, 12:02 PM
eger eger is offline
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.

Reply With Quote
Sponsored Links
  #4  
Old 10-30-2008, 12:11 PM
sabarishks sabarishks is offline
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 ?

Reply With Quote
  #5  
Old 10-30-2008, 12:16 PM
Panopta Panopta is offline
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

Reply With Quote
  #6  
Old 10-31-2008, 01:37 AM
senne senne is offline
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?

Reply With Quote
  #7  
Old 10-31-2008, 02:05 AM
jphilipson jphilipson is offline
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

Reply With Quote
  #8  
Old 10-31-2008, 02:43 AM
blessen blessen is offline
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" >

Reply With Quote
  #9  
Old 10-31-2008, 06:23 AM
barry[CoffeeSprout] barry[CoffeeSprout] is offline
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

Reply With Quote
  #10  
Old 10-31-2008, 02:59 PM
linux-tech linux-tech is offline
<?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

Reply With Quote
  #11  
Old 11-01-2008, 09:51 AM
barry[CoffeeSprout] barry[CoffeeSprout] is offline
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)

Reply With Quote
  #12  
Old 11-05-2008, 11:54 AM
sabarishks sabarishks is offline
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" >

Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Pingdom Talks Top Web Hosting Cities and Countries Web Hosting News 2013-03-27 18:49:54
Whistleblower Site Cryptome Hacked, Infects PCs with Drive-By Exploits Web Hosting News 2012-02-14 14:48:24
Control Panel cPanel Launches New Apache Configuration Script Web Hosting News 2011-12-28 19:41:39
Web Host eApps Offers Control Panel ISPmanager for Free Web Hosting News 2011-12-07 21:48:20
Web Host JaguarPC Adds Auto-Installer Softaculous to Hosting Plans Web Hosting News 2011-07-27 18:55:46


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?