Operating System - HP-UX
1833702 Members
3383 Online
110062 Solutions
New Discussion

Re: Watch for process and email if not present -

 
SOLVED
Go to solution
Rob Johnson_3
Regular Advisor

Watch for process and email if not present -

I would like to cron a script that does a ps and looks for a paticular process and if not present, email me. How could I do something like this?

4 REPLIES 4
James R. Ferguson
Acclaimed Contributor
Solution

Re: Watch for process and email if not present -

Hi Rob:

Here's a general purpose one. You can create a 'cron' task for it. Assume that you want to look for a process called "myproc" and mail/page if it isn't running:

# [ -z "`UNIX95= ps -C myproc -o pid= -o comm=`" ] && mailx -s "myproc" is not running!" root < /dev/null

Change the 'myproc' to match the basename of the process that you're interested in monitoring. As shown, this generates a mail to 'root'.

Regards!

...JRF...
Pete Randall
Outstanding Contributor

Re: Watch for process and email if not present -

ps -ef ${PROCNAME} > /tmp/proc.out
if [ `wc -l /tmp/proc.out | awk '{ print $1 }'` -gt 2 ]
then

echo "The process isn't running!" >> /tmp/mail.out
mail me@myserver < /tmp/mail.out

Something like that should do it.


Pete

Pete
Peter Godron
Honored Contributor

Re: Watch for process and email if not present -

Rob,
possible correction to Petes answer:
(No points please)

ps -ef | grep ${PROCNAME} | grep -v grep > /tmp/proc.out
if [ `wc -l /tmp/proc.out | awk '{ print $1 }'` -lt 1 ]
then
echo "The process isn't running!" >> /tmp/mail.out
mail me@myserver < /tmp/mail.out
fi
Pete Randall
Outstanding Contributor

Re: Watch for process and email if not present -

Peter,

You deserver more than 5 points for that - I lost my grep somewhere!!

Actually, we use an alias for "ps -ef |grep" that we call "psg", and I messed up the translation. Thanks for catching that.


Pete

Pete