LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-04-2006, 03:28 PM   #1
XaViaR
Member
 
Registered: Dec 2004
Distribution: RHEL, CentOS, SuSE
Posts: 170

Rep: Reputation: 31
Restart Service Bash Script


Hello,

I am trying to create a bash script that will start a service if the service stops for some reason.
Is this possible?

I know that I will have to run this script as a cron, and I will probably check the status of this service every five minutes or so.

Does anyone have a script like this handy? If so, could you please paste it? :-)

Thanks if advance!
 
Old 01-04-2006, 04:51 PM   #2
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Rep: Reputation: 32
You are completely right about the cron daemon. So I assume you know how to set up it to start the following script at 5 minutes
Well, I could give you a perl script If that suits you.
Code:
#!/usr/bin/perl
$pid = `ps -Af| grep your_program|grep -v grep|awk '{print $2}'`;
chomp $pid;
if ($pid eq ''){
        system("a_line_that_starts_your_program with some arguments &"); # let start your program
}
else { #program is running
}

Last edited by ivanatora; 01-04-2006 at 04:52 PM.
 
Old 01-04-2006, 05:35 PM   #3
XaViaR
Member
 
Registered: Dec 2004
Distribution: RHEL, CentOS, SuSE
Posts: 170

Original Poster
Rep: Reputation: 31
Thanks! You are right...I know how to setup the cron job. :-)

Thanks again!
 
Old 01-04-2006, 07:32 PM   #4
XaViaR
Member
 
Registered: Dec 2004
Distribution: RHEL, CentOS, SuSE
Posts: 170

Original Poster
Rep: Reputation: 31
For those who are interested, I converted ivanatora script to bash. I hope this helps someone! Thanks again ivanatora!

# -- Copy below here --

#!/bin/sh

#--------------------------------------------------------------------------
# This Bash script that will check to see if a serivce is started. If not,
# then it will start the service.
#--------------------------------------------------------------------------

# Checking for PID of service and storing it in a variable
#
PID=`ps -A | grep kaid | awk '{print $1}'`

# Searching for PID. If does not exist, then starting service.
#
if [ "$PID" = '' ] ; then
/etc/init.d/kaid
else
echo "Service is already started"
fi

# -- End of Script --

# -- Copy Above here --
 
Old 01-05-2006, 02:10 AM   #5
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Rep: Reputation: 32
You are most welcome
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
"service __ restart" bash command isn't working case1984 Linux - Newbie 9 04-10-2008 02:35 PM
Automatically Restart Service tulip4heaven Linux - General 1 04-23-2005 02:11 AM
bash script to stop isdn service after x mintus? paul.nel Programming 4 03-11-2004 10:01 AM
Starting a service in a bash script AMMullan Programming 3 02-19-2004 11:06 AM
bash script for network restart munkeh Programming 8 11-10-2003 05:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:52 PM.

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