LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-30-2008, 09:53 AM   #1
sabarishks
LQ Newbie
 
Registered: Aug 2008
Posts: 21

Rep: Reputation: 15
Script to restart services, if they are down.


Hello Gurus,

I am desperately looking for all your helps. This is a most importane thing in my life.

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" >&2
echo "Starting Mysql using customized script" >& 2
/etc/init.d/apache2 start
fi

Last edited by sabarishks; 10-30-2008 at 09:55 AM.
 
Old 10-30-2008, 09:55 AM   #2
sabarishks
LQ Newbie
 
Registered: Aug 2008
Posts: 21

Original Poster
Rep: Reputation: 15
Both the scripts work for one service. I need a single script which can monitor several services.

please help.......
 
Old 10-30-2008, 07:29 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You do realize you've mixed MySQL and Apache in the same file there??
Anyway, you could abstract the names of daemons out into eg a file and loop through reading that.
Ditto the restart cmds.
If you've got a lot of systems, maybe look at Nagios.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which Services Restart Network? cmnorton Linux - General 4 03-09-2008 08:35 AM
Script to restart application services on remote systems vinayhr Programming 3 09-04-2006 12:37 AM
command to restart services djc Linux - Newbie 3 03-31-2004 02:04 PM
Keyboard and mouse services, how to restart shelby Linux - Hardware 0 03-13-2003 02:05 PM
How do I restart services in Linux rytskyo Linux - Newbie 3 05-06-2002 07:27 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 08:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration