Operating System - Linux
1748223 Members
4703 Online
108759 Solutions
New Discussion юеВ

shell script for tracking processes

 
SOLVED
Go to solution
Shivkumar
Super Advisor

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
13 REPLIES 13
James R. Ferguson
Acclaimed Contributor
Solution

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...

Yogeeraj_1
Honored Contributor

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)
Arunvijai_4
Honored Contributor

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"
Muthukumar_5
Honored Contributor

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!
Muthukumar_5
Honored Contributor

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!
Arturo Galbiati
Esteemed Contributor

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
Bill Hassell
Honored Contributor

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.


Bill Hassell, sysadmin
Bill Hassell
Honored Contributor

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=)"


Bill Hassell, sysadmin
Tom Satinet
Frequent Advisor

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!