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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-04-2006, 03:28 PM
|
#1
|
Member
Registered: Dec 2004
Distribution: RHEL, CentOS, SuSE
Posts: 170
Rep:
|
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!
|
|
|
01-04-2006, 04:51 PM
|
#2
|
Member
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459
Rep:
|
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.
|
|
|
01-04-2006, 05:35 PM
|
#3
|
Member
Registered: Dec 2004
Distribution: RHEL, CentOS, SuSE
Posts: 170
Original Poster
Rep:
|
Thanks! You are right...I know how to setup the cron job. :-)
Thanks again!
|
|
|
01-04-2006, 07:32 PM
|
#4
|
Member
Registered: Dec 2004
Distribution: RHEL, CentOS, SuSE
Posts: 170
Original Poster
Rep:
|
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 --
|
|
|
01-05-2006, 02:10 AM
|
#5
|
Member
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459
Rep:
|
You are most welcome 
|
|
|
All times are GMT -5. The time now is 04:41 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|