Re: shell script for tracking processes (698 Views)
Reply
Super Advisor
Shivkumar
Posts: 1,207
Registered: ‎05-22-2005
Message 1 of 14 (698 Views)
Accepted Solution

shell script for tracking processes

Dear Sirs,

We are running an application on hpux11i and it runs 2 processes.

I want to know the shell script which can send me an email if the processes are stopped running.

I am not a seasoned shell scriptor, so your help is appriable.

Thanks,
Shiv
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 14 (698 Views)

Re: shell script for tracking processes

Hi Shiv:

You could create a 'cron' task for the following. Assume that you want to look for a process called "myproc" and email/page if it isn't running:

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

You could replicate this for you second process too.

Regards!

...JRF...

Honored Contributor
Yogeeraj_1
Posts: 4,613
Registered: ‎11-25-2001
Message 3 of 14 (698 Views)

Re: shell script for tracking processes

hi,

for critical processes, you can also include the coding for the processes to restart also.

of course, you should identified all possible scenarios that can cause your processes to stop..

kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Honored Contributor
Arunvijai_4
Posts: 4,194
Registered: ‎04-07-2005
Message 4 of 14 (698 Views)

Re: shell script for tracking processes

Hi Shiv,

You can just use ps -ae |grep -i "process_name" and if [ $? >0 ] mailx -s "Process_name not running"

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 5 of 14 (698 Views)

Re: shell script for tracking processes

Do you want to run it in continous manner or only one time?

One time:

ps -ef | grep -i | mailx -s "Process Info" ID

--
Muthu
Easy to suggest when don't know about the problem!
Honored Contributor
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 6 of 14 (698 Views)

Re: shell script for tracking processes

To use for all process requirement:

#!/bin/ksh
PROCESS_NAME=
SLEEP_TIME=60 # 1 minute

while [[ 1 ]]
do

ps -ef | grep -qi "${PROCESS_NAME}" | mailx -s "Process @ $(date)" ID

sleep ${SLEEP_TIME}

done

# END
exit 0

--
Muthu
Easy to suggest when don't know about the problem!
Esteemed Contributor
Arturo Galbiati
Posts: 830
Registered: ‎02-10-2003
Message 7 of 14 (698 Views)

Re: shell script for tracking processes

Hi Shiv,
something like this
PROCESS_COUNT=$(ps -fu <script user> | grep <script_name> | grep -v grep)
case $PROCESS_COUNT in
0) TITLE=
echo "ERR: <script_name is not running"|mailx -s $TITLE
;;
1) #OK
;;
*) TITLE=
echo "ERR: more than 1 process is running"|mailx -s $TITLE
;;
esac

HTH,
Art
Honored Contributor
Bill Hassell
Posts: 14,226
Registered: ‎05-29-2000
Message 8 of 14 (698 Views)

Re: shell script for tracking processes

James' script is the preferred method as it does not use grep to find a process. grep and ps can make a lot of mistakes since grep does not limit it's comparison to a specific field.

However, all scripts that monitor the system to provide notifications will cause big email problems if they do not stop or slow down once the notification has been sent. You can put James' 1-liner into a loop that runs until a notification is made:

#!/usr/bin/sh
set -u
export PATH=/usr/bin
# $1 is the process to monitor
if [ $# -lt 1 ]
then
echo "need MYPROC"
exit 1
fi

MYPROC=$1
SYSADM=root # replace root with another email addr
MONSECS=30
RETRYSECS=3600


while :
do
MYSTATUS="$(UNIX95= $MYPROC -o pid= -o comm=)"
if [ -z "$MYPROC" ]
then
mailx -s "$MYPROC is not running!" $SYSADM
sleep $RETRYSECS
fi
sleep $MONSECS
done

Now start the script in the background and it will run forever, but will provide the email warning once (within 30 seconds) of the failure, and then repeat the message every hour until the process is fixed.
Honored Contributor
Bill Hassell
Posts: 14,226
Registered: ‎05-29-2000
Message 9 of 14 (698 Views)

Re: shell script for tracking processes

Whoops, some text is missing on the MYSTATUS line. It should read:

MYSTATUS="$(UNIX95= ps -C $MYPROC -o pid= -o comm=)"
Frequent Advisor
Tom Satinet
Posts: 48
Registered: ‎08-18-2005
Message 10 of 14 (698 Views)

Re: shell script for tracking processes

Hello,

I am also interesting in this. But i find that $MYSTATUS is always null.

What might i be doing wrong?

I presume the script is supposed to be "-z $MYSTATUS" rather than $MYPROC.

I am aware of UNIX95 but ps -C.... is always null... :-(

this would be most useful!
Honored Contributor
Bill Hassell
Posts: 14,226
Registered: ‎05-29-2000
Message 11 of 14 (698 Views)

Re: shell script for tracking processes

The -C option provides an exact process name match. So if you were looking for /usr/bin/vi, the command would be:

UNIX95= ps -f -C vi

and to store the results of the ps search:

MYSTATUS=$(UNIX95= ps -f -C vi)
echo $MYSTATUS

Be sure you do not put a pathname (ie, -C /usr/bin/vi) because that is not the process name. The process name is just the last part of a pathname.
Valued Contributor
paolo barila
Posts: 193
Registered: ‎03-31-2003
Message 12 of 14 (698 Views)

Re: shell script for tracking processes

Hi,

check it out also

# ps -efx | grep

to see all parameters of a process running,

very useful with java processes.


Pablo
share share share
Exalted Contributor
Steven E. Protter
Posts: 33,806
Registered: ‎08-15-2002
Message 13 of 14 (698 Views)

Re: shell script for tracking processes

Shiv,

I agree with Bill. JRF's script is the most elegant and efficient because it does it all on one line.

Note:
Do not set the UNIX95 variable by default in /etc/profile, .profile or other profiles, unless you create a special user for this.

A VERY common gotcha is setting this variable by default. It interferes with SD/UX when you sh a patch depot, it triggers checksum failures.

I will admit that I fell victim to this very mistake some years ago.

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
Frequent Advisor
Tom Satinet
Posts: 48
Registered: ‎08-18-2005
Message 14 of 14 (698 Views)

Re: shell script for tracking processes

Hi,

This is great. I have got this working now. We have a stupid electronic invoicing system that send out emails. but it dies if it looses connection to the smtp server even for a second. don't ask why it can't wait..... p poor code....

anyway, I think i will run this in a start up script and monitor the process. Thanks for the warning on UNIX95. i think i will just run this as root, but set the variable in the script.

many thanks:-D
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.