Operating System - Linux
1752421 Members
5944 Online
108788 Solutions
New Discussion юеВ

Re: Need to monitor the existance of a process

 
SOLVED
Go to solution
Klaas D. Eenkhoorn
Regular Advisor

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


11 REPLIES 11
Jeff_Traigle
Honored Contributor
Solution

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
Peter Godron
Honored Contributor

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

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...
A. Clay Stephenson
Acclaimed Contributor

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.
Klaas D. Eenkhoorn
Regular Advisor

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

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.


Bill Hassell, sysadmin
Yogeeraj_1
Honored Contributor

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)
Klaas D. Eenkhoorn
Regular Advisor

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
Peter Godron
Honored Contributor

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.