- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script to email if process stops -
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 04:07 AM
02-04-2006 04:07 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 04:23 AM
02-04-2006 04:23 AM
SolutionYou 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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 04:23 AM
02-04-2006 04:23 AM
Re: Script to email if process stops -
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 04:41 AM
02-04-2006 04:41 AM
Re: Script to email if process stops -
There are many ways to do a script for this,
The easy one is
TEST=$(ps -ef |grep
if [[ $TEST -ne 1 ]] ** 1 is number of process you would like to check.
then
mail -x "PROC FAILED" rob@johnson.com
Chan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 04:47 AM
02-04-2006 04:47 AM
Re: Script to email if process stops -
Thanks for everyone's input!!!!!!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 05:46 AM
02-04-2006 05:46 AM
Re: Script to email if process stops -
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:~>
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 07:33 AM
02-04-2006 07:33 AM
Re: Script to email if process stops -
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
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 07:43 AM
02-04-2006 07:43 AM
Re: Script to email if process stops -
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 07:55 AM
02-04-2006 07:55 AM
Re: Script to email if process stops -
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2006 08:47 AM
02-04-2006 08:47 AM
Re: Script to email if process stops -
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!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2006 02:19 PM
02-05-2006 02:19 PM
Re: Script to email if process stops -
#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 01:10 AM
02-06-2006 01:10 AM
Re: Script to email if process stops -
# 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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2006 04:20 AM
02-06-2006 04:20 AM
Re: Script to email if process stops -
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 ;-)