Operating System - Linux
1825882 Members
2970 Online
109689 Solutions
New Discussion

Re: Shell Scripts for monitoring and starting the process

 
SOLVED
Go to solution
Shivkumar
Super Advisor

Shell Scripts for monitoring and starting the process

Hi,

I want to monitor a process and whenever it stops we should receive email notification and execute the command
for starting the process.

For example: i want to monitor the process name "tulip-web-admin". Once it is found that
the process is not running then it should send email notification and start /opt/somedirectory/tulip.sh script.

And we should receive the email notification that process has been re-started.

Can any one suggest korn shell script for this ?

Thanks,
Shiv
11 REPLIES 11
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Shell Scripts for monitoring and starting the process

Okay, here goes:

#!/usr/bin/sh (or #!/usr/bin/ksh if you insist)

typeset PROCESS-"tulip-web-admin"
typeset -i STAT=0

UNIX95=1 ps -C "${PROCESS}" -o pid='' > /dev/null 2>&1
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
/opt/somedirectory/tulip.sh # you may ned a & to start in background ?
STAT=${?}
echo "Restarted" | mail mickey@mouse.com
fi
exit ${STAT}

You then cron this script to run every 5 minutes or so.

Generally this is a Mickey Mouse solution because it appears that you are trying to fix the symptoms rather than fixing the problem itself --- ie, why is the process dying?
If it ain't broke, I can fix that.
Shivkumar
Super Advisor

Re: Shell Scripts for monitoring and starting the process

Clay, Is it possible to check continuously without putting this script in the crobtab ?
A. Clay Stephenson
Acclaimed Contributor

Re: Shell Scripts for monitoring and starting the process

Yes, although the typical approach there is to create an inittab entry and make it a 'respawn' task. Each time the task exits, init automatically restarts it.

There are several ways to do what you want but continuously running ps could impose an abnormal load on the system.

-------------------------------------------
typeset PROCESS-"tulip-web-admin"
typeset -i STAT=0
typeset -i DELAY=10

while [[ 1 -eq 1 ]]
do
UNIX95=1 ps -C "${PROCESS}" -o pid='' > /dev/null 2>&1
STAT=${?}
if [[ ${STAT} -ne 0 ]]
then
/opt/somedirectory/tulip.sh # you may ned a & to start in background ?
STAT=${?}
echo "Restarted" | mail mickey@mouse.com
fi
sleep ${DELAY}
done
exit ${STAT}

---------------------------------------


This would check every ${DELAY} seconds.

Here's another approach leveraging wait (and is a bit cleaner):

typeset -i CHILD=0
while [[ 1 -eq 1 ]]
do
/opt/somedirectory/tulip.sh &
CHILD=${!}
wait ${CHILD}
echo "Restarted" | mail mickey@mouse.com
done

Either of these scripts should be put set up as an rc'ed command so that this is the sole means of starting the script. HOWEVER, you still seem to be fixing symptoms of a bad application.

If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Shell Scripts for monitoring and starting the process

By the way, the production version of these scripts should have a trap that would 1) kill the tulip-web-admin process and 2) terminate the monitor script. This part is left as an exercise.
If it ain't broke, I can fix that.
Veeru_1
New Member

Re: Shell Scripts for monitoring and starting the process

Hi!

I have been developing a new tool - sentinel - which does what you want (and more) and I am looking for beta testers for it.

Could you send me your contact so that I could give you instructions on how to use it? It comes bundled with WDB 5.6 and later available on the web, but is not an officially supported tool until we can understand its use needs.

Thanx!
Veeru@hp.com
Shivkumar
Super Advisor

Re: Shell Scripts for monitoring and starting the process

Hi Veeru,

Please email me on:

sksonkar@gmail.com

Thanks,
Shiv
James R. Ferguson
Acclaimed Contributor

Re: Shell Scripts for monitoring and starting the process

Hi Shiv:

Shame on you! You asked this very question last year. If you examine the very rich number of question and responses associated with YOUR profile:

https://forums1.itrc.hp.com/service/forums/publicProfile.do?userId=CA1283218&forumId=1

...then you would find:

https://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=996430

...from January 2006 which answered this question!!!

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Shell Scripts for monitoring and starting the process

>JRF: You asked this very question last year

Except I noticed it and told Veeru about it. :-)
Shivkumar
Super Advisor

Re: Shell Scripts for monitoring and starting the process

James,

I changed my job and was not able to trace the earlier responses. Unfortunately, after posting the question i came to know how to look for the previous responses.

I apologize with the forum members.

Best Regards,
Shiv
Steven E. Protter
Exalted Contributor

Re: Shell Scripts for monitoring and starting the process

Shalom Shiv,

One of the things I use my forums profile is to find good threads from the past.

When I find a thread that is really good, I add it to my ITRC favorites. Two reasons:

1) So I can use the thread to handle a situation I don't often have to perform. Memory aid really.

2) So I can cut and paste code or a thread link for answering ITRC questions.

An example of number 2 is the mirror with mirror/ux post I often use. It comes from my forums favorite.

Don't worrry about the mistake, I make more than my share posting replies.

No need to point this post, just don't want you to feel so bad.

All is forgiven. By me, I don't speak for others.

The question did seem very familiar to me.

:-)

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
James R. Ferguson
Acclaimed Contributor

Re: Shell Scripts for monitoring and starting the process

Hi (again) Shiv:

Remembering every detail, or even how to do something, is less important than learning *how* to find an answer.

Sometimes the "how" is searching manpages and *trying* to write something that fits your needs and solves a problem. Perfection is an endless journey.

The Chinese say, "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime."

Take my gentle admonishment in my first response in the manner I intended it: an encouragement to fish.

You need not award points for this.

Regards to you, Shiv!

...JRF...