Need to monitor the existance of a process (129 Views)
Reply
Regular Advisor
Klaas D. Eenkhoorn
Posts: 108
Registered: ‎11-04-2002
Message 1 of 12 (129 Views)
Accepted Solution

Need to monitor the existance of a process

All,

I'm an idiot asking this, i know, but i need to monitor a process of it's existance.
A monitor script must run in a loop until the monitored process has finished. then it has to exit.

while ....

ps -ef | grep PROCESS


...


Please fill in the blanks.
I have a bit of a programmers block at the moment.

Kl@@s


Please use plain text.
Honored Contributor
Jeff_Traigle
Posts: 1,354
Registered: ‎03-04-2004
Message 2 of 12 (129 Views)

Re: Need to monitor the existance of a process

One approach...

while [ $(ps -ef | grep PROCESS | grep -v grep) != "" ]
do
sleep 5
done
--
Jeff Traigle
Please use plain text.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 3 of 12 (129 Views)

Re: Need to monitor the existance of a process

Klaas,
many solutions possible.

#!/usr/bin/sh
a=1
process="fred"
while [ $a -eq 1 ]
do
a=`ps -ef | grep $process | grep -v grep | wc -l`
done
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 4 of 12 (129 Views)

Re: Need to monitor the existance of a process

Hi:

Whatever you do, don't rely on a 'grep' of the process table to select processes by name. The UNIX95 (XPG4) option with 'ps' allows you to select a process by its basename, thereby eliminating false matches.

This would work. Change the value of NAME to match your process.

# cat ./monitor
#!/usr/bin/sh
NAME=sleep
PID=`UNIX95= ps -C ${NAME} -o pid=`
while [ ! -z "${PID}" ]
do
PID=`UNIX95= ps -C ${NAME} -o pid=`
if [ -z "${PID}" ]; then
break
else
echo "alive!"
sleep 5
fi
done
echo "exiting..."

Regards!

...JRF...
Please use plain text.
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 5 of 12 (129 Views)

Re: Need to monitor the existance of a process

Bear in mind that even if you use the XPG4 behavior of ps to allow the use of the -C argument that it is still possible to have multiple instances of the same program running. For example (and it's a dumb example), consider running two instances of vi on the same text file. Everything looks identical so which vi do you really want to monitor? I would turn the world around a bit and let the monitor script actually start the process and then use the kill -0 command to check for the existance of the process.

Something like this:

#!/usr/bin/sh

typeset -i PROC_PID=0
typeset -i KILL_STAT=0

typeset MY_PROC=/xxx/yyy/zzz.exe # the process to start and monitor

${MY_PROC} & # start in background
PROC_PID=${!} # capture its PID
while [[ ${KILL_STAT} -eq 0 ]]
do
kill -0 ${PROC_PID} 2>/dev/null
KILL_STAT=${?}
if [[ ${KILL_STAT} -eq 0 ]]
then
sleep 10
else
echo "Process ${PROC_PID} has terminated."
fi
done


kill -0 PID returns a zero exit status if PID is a valid process ID and non-zero if not.
If it ain't broke, I can fix that.
Please use plain text.
Regular Advisor
Klaas D. Eenkhoorn
Posts: 108
Registered: ‎11-04-2002
Message 6 of 12 (129 Views)

Re: Need to monitor the existance of a process

Well the grep issue is not that issue for me.
I need to start a command to list the batchpocesses of a program and based on that output i have to check if a specific batchprocess is still running or finished.

Sorry for the misslead with the line ps -ef but i had to think of a simple exsample to point my problem.

My real problem is, before the backup of a program i have to start a triggerprogram for the batchprocesses within the application to stop (not at or cron).
But this program is also a batch program so i have to wait till this program kicks-in and does it's work and after the last program exits the batch the backup script must continue with the actual backup.

So i have to wait for a program with a delay to shut down other programs with a delay, and how big that delay is i do'nt now and i get no reply if the programs are finished or not.
The only thing i have is a list of currently running batch processes.
Do i have to be original on this or not . . .

Well, Jeff's solution helped me so far, tomorrow i'll know more.

Thanks for the replies.
Please use plain text.
Honored Contributor
Bill Hassell
Posts: 14,178
Registered: ‎05-29-2000
Message 7 of 12 (129 Views)

Re: Need to monitor the existance of a process

This is a common problem but in a production environment, I would not allow no-status programs to run like that. The whole concept of semaphores, or status files eliminates the large uncertainty in monitoring programs like this. The usual solution is to wait longer than the normal completion time (completely undependable). The next is to watch the programs go away as in your example. But what if the program(s) crash, or are killed or do not run correctly?

I would strongly recommend that the other process(es) be rewritten to report an exit status, perhaps through a simple text file. And the monitoring script needs to perform a sanity check in case no completion file is created after several hours.

If you write code with the idea that everything is going to fail, you won't be disappointed.
Please use plain text.
Honored Contributor
Yogeeraj_1
Posts: 4,613
Registered: ‎11-25-2001
Message 8 of 12 (129 Views)

Re: Need to monitor the existance of a process

hi,

as mentioned by Bill above, you should ensure that the programs returns an exit status and all you have to do is write a notification programs that informs you. Can be a mail notification or just modification of a status file or table column in a database.


e.g.
SCRIPT_STATUS=`
if [ "$SCRIPT_STATUS" != "SUCCESSFUL" ]
then
echo "Error running script <script name>"|mailx -s "error notification" youraddress@domain.mu
fi

hope this helps too!
kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Please use plain text.
Regular Advisor
Klaas D. Eenkhoorn
Posts: 108
Registered: ‎11-04-2002
Message 9 of 12 (129 Views)

Re: Need to monitor the existance of a process

Well Bill you ar right but the writers of the application did not build any status replies outide the application for batchjobs other than in the application itself.
Outside the applications you can not tell if or how the batchjob ended.
There is only a fuction to view a list of current processes of the application.

Probably there would be a way to digg in the oracle database underneath to see what batchjobs are running and if they are finished but i'm no oracle guru.

There solution was to stop the batchjobs with the trigger program from cron for lets say 10 minutes in advance of the backup and if this was not sufficient i had to increase that time.
That option is mot acceptable for me because the batchprogram supplies information to otherprograms like SAP and that would mean that these SAP applications can also not be used during that time.

Well last night all seemd to go well so i'll see what happns tonight.

Klaas
Please use plain text.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 10 of 12 (129 Views)

Re: Need to monitor the existance of a process

Klaas,
could you please update the thread with the result of your overnight run.

Another idea I had was to use the wait command to wait for the completion of a specific process before the next process starts.
Please use plain text.
Regular Advisor
Klaas D. Eenkhoorn
Posts: 108
Registered: ‎11-04-2002
Message 11 of 12 (129 Views)

Re: Need to monitor the existance of a process

It worked, the process is correctly monitored and the script continues after wards.

Thanks for the help !

Kl@@s
Please use plain text.
Regular Advisor
Klaas D. Eenkhoorn
Posts: 108
Registered: ‎11-04-2002
Message 12 of 12 (129 Views)

Re: Need to monitor the existance of a process

This works:

print_msg "Stopping Production"
su - user -c '${TSAPPLROOT}/bin/startcronjob ${TSAPPLROOT} SERVICE'
if [ $? -eq 0 ]
then
print_msg "Stop Production scheduled succesfully"
else
print_msg "Stop Production scheduling failed"
fi

print_msg "Waiting for Production to finish ..."

TIMEOUT=180 # 3 Minutes time-out
FOUND=1
while [ $FOUND -ne 0 ]
do
FOUND=`/usr/bin/su - user -c showcom | /usr/bin/grep SERVICE | /usr/bin/wc -l`
sleep 2
echo $FOUND
if [ ${TIMEOUT} -le "0" ]
then
echo "Timed-out"
FOUND=0
else
TIMEOUT=`expr ${TIMEOUT} - 1`
fi
done

Thanks.
Please use plain text.
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