Operating System - HP-UX
1844398 Members
4103 Online
110232 Solutions
New Discussion

Re: Script to email if process stops -

 
SOLVED
Go to solution
Rob Johnson_3
Regular Advisor

Script to email if process stops -

To start, my scripting skills are pretty much zero...

I want to be able to do, via a scheduled cron job, a ps and look for a particular process and when the particular PID isn't found, I would like the to kick off the mail command to notify a person or persons that the process has stopped. I don't necessarily want to restart the process because I'm not the administrator; I just want to know that's it's not running.

Can someone help me out?
12 REPLIES 12
Steven E. Protter
Exalted Contributor
Solution

Re: Script to email if process stops -

Shalom Rob,

You have come to the right place.

The basic email code, though its a little overengineered for your purpose is here:

http://www.hpux.ws/mailfile2

All you need to add is a few lines of codes to decide if the process is running or not.

For sake of argument, lets say the process is named schmobagel

Here is the additoinal code.

procname=schmobagel
procs=$(ps -ef | grep $procname | grep -v grep| wc -l)

This code produces the number of processes named schmobagel running on your system.

if [ $procs -eq 0 ]
then
mailfile2 <5 parameters including to and from email address>
else
echo "All is well in $procname land"
fi

You can lift the email code out of mailfile2 and simplify the script if you wish.

mailfile2 is designed to send an attachment which may be useful in certain concepts. is a production script that has been extensively tested worldwide.

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: Script to email if process stops -

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...
Chan 007
Honored Contributor

Re: Script to email if process stops -

Rob,

There are many ways to do a script for this,

The easy one is

TEST=$(ps -ef |grep |grep -e grep |wc -l)
if [[ $TEST -ne 1 ]] ** 1 is number of process you would like to check.
then
mail -x "PROC FAILED" rob@johnson.com

Chan

Rob Johnson_3
Regular Advisor

Re: Script to email if process stops -

10 pts to each of you.

Thanks for everyone's input!!!!!!!!

Rob Johnson_3
Regular Advisor

Re: Script to email if process stops -

Ok. I booted my PC up on the Linux side and tried to make this work by watching the cupsd process. All I have to do to see if I can make this work is kill that process and this should be a good test.

I created a file called pswatchtest, did a chmod on it to make it executable but get the error below when I try to run it.

What am I doing wrong here? Remember, my scripting skills are zero and my Unix skills arenâ t much better!!!

robby@linux:~> ./pswatchtest
./pswatchtest: line 5: syntax error: unexpected end of file
robby@linux:~> cat pswatchtest
TEST=$(ps -ef |grep cupsd|grep -e grep |wc -l)
if [[ $TEST -ne 1 ]]
then
mail -x "Test process FAILED" getwithrob@hotmail.com
robby@linux:~>
Steven E. Protter
Exalted Contributor

Re: Script to email if process stops -

Rob,

mailx in Unix is mail -s in Linux. don't ask me why.

Another thing to know is that if you kick the job from cron you must either provide the full command path or create a path for the script to find binaries.

PATH=/usr/bin:/usr/local/bin

That's an example.

I generally provide my cron scripts full path, though that risks them being broken when the next OS comes along.

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: Script to email if process stops -

Hi (again) Rob:

All scripts should begin with a "shebang" to declare the intepreter they intend to use.

A "shebang" is the characters "#!" that begin the first line of any Unix script. They are followed immediately by the path and name of the interpreter that should be used to run (execute) the script that follows.

If this is a shell script it will be (variously, but not limited to):

#!/bin/sh
#!/usr/bin/sh
#!/usr/bin/ksh
#!/usr/bin/bash

If this is a perl script you would see something like:

#!/usr/bin/perl
#!/opt/bin/perl

Since we have been talking about Unix shell scripts, the first set applies. In HP-UX, scripts are generally written in the POSIX shell (/usr/bin/sh). On HP-UX, '/bin' is really a link to '/usr/bin', so you could use either path (although '/bin' is deprecated).

The script that you attempted to run gave the error, "line 5: syntax error: unexpected end of file".

You will notice that the script was only 5-lines long. The "unexpected EOF" indicates that without the proper 'shebang' it was unclear which interpreter should be run.

Add, either '#!/bin/sh' or '#!/usr/bin/sh' to your script as the first line and try again.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Script to email if process stops -

Hi Rob:

Well, I missed the most obvious error!

Shell syntax for the 'if' statement says that an 'if' must have closure. That is done by saying "fi" to end the scope of the condition. Your script should look like:

#!/bin/sh
TEST=$(ps -ef |grep cupsd|grep -e grep |wc -l)
if [[ $TEST -ne 1 ]]
then
mailx -s "Test process FAILED" getwithrob@hotmail.com < /dev/null
fi

I added the /dev/null to signal 'mailx' that there is no input file; only a simple subject line.

Regards!

...JRF...
Rob Johnson_3
Regular Advisor

Re: Script to email if process stops -

So here's what I ended up doing:

I scrapped trying to make the example I got from Chan 007 work and went with the example Steven E Protter gave. I didn't use the sample mail script but instead just used the mailx command because I knew I could do this -
mailx -v rob@johnson.com < /etc/hosts
to mail the contents of a file on this linux box.

I made a text file called test and put one line in it. That line was "Process has stopped!" The reason I did this is because Iâ m not too sure of the syntax on the mailx command but knew how to use the mailx and mail the contents of file using the mail syntax above.

I tried this and it worked:

#!/bin/sh
procname=cups
procs=$(ps -ef | grep $procname | grep -v grep| wc -l)

if [ $procs -eq 0 ]
then
mailx -v getwithrob@hotmail.com < test
else
echo "All is well in $procname land"
fi

It's not perfect but it's a start and it works.

Again, thanks for you guys' help!!!
Muthukumar_5
Honored Contributor

Re: Script to email if process stops -

Try as,

#!/bin/sh
procname=cups
procs=$(ps -ef | grep ${procname} | grep -v grep| wc -l)

if [ $procs -eq 0 ]
then
echo "Process has stopped!" | mailx -v getwithrob@hotmail.com
else
echo "All is well in $procname land"
fi

# end
exit 0

It will work properly. Setup in cron tab for scheduling.

--
Muthu
Easy to suggest when don't know about the problem!
rmueller58
Valued Contributor

Re: Script to email if process stops -

#!/bin/bash
# This scripts verify named services are available from second system
ns=`nslookup -sil www.yyy.com|head -1 |awk '{print $2}'`
if [ "$ns" != "xxx.xxx.xxx.yyy" ]
then
mail -v -s "NS Server failed lookup test`date +%Y%m%d:%H%M`" mailname@smtp.server.domain< /usr/local/bin/serverdown.message
Ralph Grothe
Honored Contributor

Re: Script to email if process stops -

Hi Rob,

though you got it already running,
and probably don't require any uncalled for
advice anymore,
just a wee annotaion regarding Linux.

On a Linux box the default shell is usually bash.
Even if you put #!/bin/sh in the Shebang,
it most likely is only a link to the bash binary.
However as the Bash manpage elaborates,
when bash is invoked as sh it is bound to run in POSIX mode.
Also it won't source rc files if it is an interactive shell.
This is fine, as for most parts you can stick to HP-UX shell syntax
(however, bash has more features, and I think array initialization uses slightly deviating syntax)

Afaik, Linux doesn't have a mailx executable
(SEP asked for what reason, well because the Linux folks started with an "extended" mail client right from the start ;-)
But you are free to set an alias like "alias mailx=mail"

Besides, Linux always has adopted the nifty stuff from other Unices.
So e.g. from Solaris they borrowed the pgrep and pkill commands.
These make parsing the proc table much easier.
Say if your proc was a Tomcat (the only Java master thread, to keep it easy) you could do something like

while (( $(pgrep java) ));do sleep 60;done;mail -s "Tomcat died?" you@yours.com

If you have a look at GNU/Linux ps manpage
you will find that you can give ps a HP-UX like behaviour by setting an environment variable PS_PERSONALITY=hpux.
But of course the Linux community deprecates this ;-)



Madness, thy name is system administration