Operating System - HP-UX
1833780 Members
2764 Online
110063 Solutions
New Discussion

Re: how to check a deamon live or dead?

 
SOLVED
Go to solution
Cem Tugrul
Esteemed Contributor

how to check a deamon live or dead?

Hi Gurus,
Let's say i have deamon and name xyz...i want to take an alert,e-mail message when this deamon stopped or killed..so how can i organize this scenario?
Please share your precious decisions...
Best Regards,
Our greatest duty in this life is to help others. And please, if you can't
11 REPLIES 11
Umapathy S
Honored Contributor

Re: how to check a deamon live or dead?

Write a script which checks for the daemon and mail you when it is down. Schedule the script using crontab.

Umapathy
Arise Awake and Stop NOT till the goal is Reached!
Mark Grant
Honored Contributor

Re: how to check a deamon live or dead?

If your daemon can run in the foreground, a nice strategy is to write a script that starts your daemon and then sleeps. It should trap "SIGCHLD". If your daemon dies, your script will receive a SIGCHLD immediately and, because of your "trap", you can do an alert.

If your daemon can not be made to run in the foreground, you'll just have to do the regular "ps | grep" thing which always seems a bit nasty to me but sometimes there's no choice.
Never preceed any demonstration with anything more predictive than "watch this"
Mobeen_1
Esteemed Contributor

Re: how to check a deamon live or dead?

Cem,
I would just do a simple script and grep for the deamon (ps -ef | grep xxxx) and mail or page myself if the daemon is not shown in the grep command above. You can use sleep to run it at regular intervals or just put it in the crontab.

Alternatively you can also look at the other means which our friends have illustrated in prior posts and see what best fits your needs

rgds
Mobeen
Cem Tugrul
Esteemed Contributor

Re: how to check a deamon live or dead?

Hi everybody,
Thak's so much for your kind concern...
i can get the process name via
ps -ef|egrep -i processname and i want to write a script and put it on cron...but how??
just want you to be a little clear...please
try to give some examples...

Best Regards
Our greatest duty in this life is to help others. And please, if you can't
Mark Grant
Honored Contributor

Re: how to check a deamon live or dead?

ps -ef | grep -v grep | grep > /dev/null || {
mailx -s "process has died" myemail@mydomain
}

That's your script and if you called /usr/local/bin/checkdaemon you could put it in cron to run every ten minutes like this

00,10,20,30,40,50 * * * * /usr/local/bin/checkdaemon
Never preceed any demonstration with anything more predictive than "watch this"
Bill Hassell
Honored Contributor

Re: how to check a deamon live or dead?

Using a script to check on the program is a good idea but using grep will always lead to problems. grep is unable to tell the difference between user xyz and program xyz or a process that has xyz in the command line. Just put something like this in cron:

#!/usr/bin/sh
# monitor a specific program
# report if not running or more than 1 is running
# Usage: watchprog program_name

set -u
PATH=/usr/bin
MYNAME=${0##*/}

if [ $# -lt 1 ]
then
echo "\nUsage: $MYNAME program_name\n"
exit 1
fi

MYPROG="$1"
PROCLIST=$(UNIX95= ps -C $MYPROG -o pid | grep -v "PID")
if [ "$PROCLIST" = "" ]
then
echo "$MYPROG stopped $(date)" | mailx -s"$MYPROG stopped" root
exit 2
fi

PROCNUM=$(echo "$PROCLIST" | wc -l)

if [ $PROCNUM -gt 1 ]
then
echo "$MYPROG multiple copies running, PIDs= $PROCLIST $(date)" \
| mailx -s"$MYPROG more than 1 copy" root
exit 3
fi

exit


Put this in cron, perhaps running every 10 minutes:

0,10,20,30,40,50 * * * * /usr/contrib/bin/monprog mydaemon


Bill Hassell, sysadmin
Cem Tugrul
Esteemed Contributor

Re: how to check a deamon live or dead?

Hi again,
i am really new at scripting so could you please check me..
#!/usr/bin/sh
proc_name=`ps -ef|grep -v grep|egrep -i rvs01|egrep -i ttaad|awk '{print $14}'`
proc_name=${proc_name:-"ttaad5206m000"}
if [ "$proc_name" = "ttaad5206m000" ]
then
exit1
#because i am serachin the proc_name ttaad5206m000
else
echo "$proc_name stopped $(date)" | mailx -s"$proc_name stopped" root
fi

so could it be the solution? or other suggestions...

Best Regards,
mailx -s "$proc_name stopped"
Our greatest duty in this life is to help others. And please, if you can't
Bill Hassell
Honored Contributor

Re: how to check a deamon live or dead?

Just copy the above script and run it like this:

monproc ttaad5206m000

The ps -ef | grep strings are not very reliable.


Bill Hassell, sysadmin
Cem Tugrul
Esteemed Contributor

Re: how to check a deamon live or dead?

Hi Bill,
First of all i would like to thank you about
your very helpful script...yes it works but
needs a little bit modifications and this is my fault given misssing information.
When i run the command;
ps -ef|egrep -i rvs|egrep -i bshell
always returns like;
rvs 23091 23090 0 09:23:51 ? 24:41 bshell (rvs01[rvs]:23090/PIPE) 23090 321 6 7 ttaad5206m000
rvs 23046 23045 0 09:23:03 ? 0:01 bshell (rvs02[rvs]:23045/PIPE) 23045 321 6 7 ttaad5206m000
This means i have to see deamon ttaad5206m000
two times but depends rvs01 and rvs02
so if i miss one of them this means i have problem and mail root or etc...
So now,How can i modify your sript?
Thank' so much again for your kind concern...
Best Regards,
Our greatest duty in this life is to help others. And please, if you can't
Bill Hassell
Honored Contributor
Solution

Re: how to check a deamon live or dead?

Well, now you'll haver to define the relationship in much more detail. First, to locate everything running as user rvs, use:

ps -f -u rvs

egrep is an expensive form of grep as it runs through extended regular expression code such as choice1|choice2|choice7 as a search criteria. For simple string matches, fgrep (or grep -F) is much faster and will allow searching for special characters like * and ? if needed.

So is the relationship that each bshell (rvs## must have a matching ttaad5206m000? It appears that each ttaad5206m000 daemon starts a bshell process based on PID and PPID. Now if ttaad5206m000 disappears, the child bshell should also disappear unless the child disconnects from the parent. So we'll need a little more detail on how the daemon and child processes are supposed to work. Perhaps the code could be fixed to prevent loss of the daemons rather than searching for them every few minutes?


Bill Hassell, sysadmin
Cem Tugrul
Esteemed Contributor

Re: how to check a deamon live or dead?

Very Good replies...Thank's...
Our greatest duty in this life is to help others. And please, if you can't