Operating System - HP-UX
1849251 Members
5055 Online
104042 Solutions
New Discussion

Script-Check if process running, if not start it up

 
SOLVED
Go to solution
Greg Hale
Advisor

Script-Check if process running, if not start it up

Can anyone recommend a script that will check to see if a process is running and if it isn't attempt to start it back up?

thanks,

Greg
5 REPLIES 5
Victor BERRIDGE
Honored Contributor
Solution

Re: Script-Check if process running, if not start it up

Hi,
I would write a script to be run by cron like:

#script
PROC=`ps -ef? grep -c ?your_proc?`

case $PROC in
1) #Process OK
;;
0) # Not there so start it
/sbin/init.d/ Sxxxesac
#exit

Good luck
All the best
Victor
Sandor Horvath_2
Valued Contributor

Re: Script-Check if process running, if not start it up

Hi !

Use ps -e instead of ps -ef and
grep ' proc_name$'.

So
if [ $(ps -e | grep ' proc_name$' | wc -l) -ne 0 ]
then
exit # process run
fi

regards, Saa

If no problem, don't fixed it.
James R. Ferguson
Acclaimed Contributor

Re: Script-Check if process running, if not start it up

Hi Greg:

Here's a hypothetical script that monitors a process that it spawns, restarting the process as soon as it dies. It gives you another example to think about:

#!/usr/bin/sh
function WHAT
{
sleep 30 & #...run in background...
}
while true #...forever...
do
WHAT
WHO=$!
echo "pid_${WHO} started"
while true
do
kill -0 $WHO 2> /dev/null #...check for the pid...
if [ $? -ne 0 ]
then
echo "\npid_${WHO} died"
break #...exit inner loop ...
else #...it's still alive...
sleep 1
echo ".\c"
fi
done
done
#_end

...JRF...
Alan Riggs
Honored Contributor

Re: Script-Check if process running, if not start it up

There are lots of ways to solve this problem. One of them is to use the init process. Anything started from inittab with a "respawn" flag is started at the specified run level, monitored, and restarted if it dies.

Why reinvent the wheel?
Steven Sim Kok Leong
Honored Contributor

Re: Script-Check if process running, if not start it up

Hi,

If you are involving the use of grep on a ps output, please remember to add a "| grep -v grep".

This is because grep itself is a process displayed with ps. If you ps -fae|grep PROC_NAME, other than the PROC_NAME process being reflected, "grep PROC_NAME" process will also be reflected. By adding "| grep -v grep" to form a "ps -fae|grep PROC_NAME|grep -v grep", you effectively filter off the display of the grep process.

Hope this helps. Regards.

Steven Sim Kok Leong
Brainbench MVP for Unix Admin
http://www.brainbench.com