LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script to verify the daemon is working if not, start the daemon (http://www.linuxquestions.org/questions/linux-newbie-8/bash-script-to-verify-the-daemon-is-working-if-not-start-the-daemon-757046/)

kishoreru 09-22-2009 01:29 PM

Bash script to verify the daemon is working if not, start the daemon
 
Hi,
I need to write a bash script to verify some xyz daemon is running are not, if not start that xyz daemon

Any help would be appreciated...

Thanks is Advance

nuwen52 09-22-2009 02:12 PM

A lot of daemons store their process ID in a file in /var/run (ex. sshd.pid for the sshd daemon). You might try looking there. Then if you find it, you can do an 'if [ -x "/proc/<PID>" ]' to see if the process directory exists. Otherwise, just a grep through a "ps -ax" for the daemon name might be enough.

catkin 09-22-2009 02:28 PM

If the xyz daemon init script is fully featured then the commands you need are
Code:

/etc/init.d/xyz status
and
Code:

/etc/init.d/xyz start
or maybe restart would be safer -- cleaning up after a crashed daemon. You can find out more by inspecting the /etc/init.d/xyz script.

lwasserm 09-22-2009 03:41 PM

You might take a look at the "start-stop-daemon" program that is used in debian and possibly other distros.

unSpawn 09-22-2009 04:11 PM

Quote:

Originally Posted by kishoreru (Post 3693183)
I need to write a bash script to verify some xyz daemon is running are not, if not start that xyz daemon

If "need to write" is not due to some class assignment then unless you're into reinventing the wheel I'd look at something like Monit instead. It'll be faster than interpreted code, more configurable than you can script in one month and it's used, supported, maintained OSS.

lutusp 09-22-2009 07:25 PM

Quote:

Originally Posted by kishoreru (Post 3693183)
Hi,
I need to write a bash script to verify some xyz daemon is running are not, if not start that xyz daemon

Any help would be appreciated...

Thanks is Advance

This is just a hint, not a complete solution (because your question sounds like homework).

Code:

# service httpd status | grep -iqP "\brunning\b" && (do something here)

kishoreru 09-22-2009 11:52 PM

Quote:

Originally Posted by lutusp (Post 3693533)
This is just a hint, not a complete solution (because your question sounds like homework).

Code:

# service httpd status | grep -iqP "\brunning\b" && (do something here)


Thanks lutusp & All

I am new to Linux and Bash may be my question sounds like a homework :-)

tikit 09-23-2009 04:29 AM

You can use init to control the service. Put this line to /etc/inittab
Code:

ap:2345:respawn:/usr/sbin/apache2
and reload the init process
Code:

kill -HUP 1
If the service stops in will be automatically started


All times are GMT -5. The time now is 10:31 PM.