Monitor and restart UNIX process

Hi all,

Tearing my hair out..!

I have a requirement to monitor and restart a unix process via a simple watchdog script.

I have the following 3 scripts that dont work for me..

script 1 (only produces 1 output if process is up or not)
-----------------------------------------------------------------

#!/bin/bash

PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/kerberos/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin

ps cax | grep tnsrec > /dev/null
#echo $?
if [ $? -eq 1 ]; then
  bash /opt/oracle/scripts/tnsrec CP10C > tnsrec.log 2>&1 &
  exit
else
  exit
fi

script 2 (only produces 1 output if process is up or not)
-----------------------------------------------------------------

#!/bin/sh

PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/kerberos/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin

TNSPROC=`ps -ef|grep tnsrec | awk '{if ($8 !~ /grep/) print $2}'`

if ! [ -n "${TNSPROC}" ] ; then
  nohup /opt/oracle/scripts/tnsrec CP10C > tnsrec.log 2>&1 &
fi

exit 0

script 3 only produces 1 output..
--------------------------------------

#!/bin/bash

PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/kerberos/bin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin

ps -ef |grep -v grep | grep tnsrec > /dev/null 2>&1

case "$?" in
  0)
  true
  ;;
  1)
  bash /opt/oracle/scripts/tnsrec CP10C > tnsrec.log 2>&1 &
esac
exit

What am I doing wrong?

Any one any ideas for a simple watchdog script..

Thanks in advance,

jd

Having said that, what is wrong here???

none of the example working for me.

Will this "nohup /opt/oracle/scripts/tnsrec CP10C > tnsrec.log 2>&1 &" work properly when u execute from commandline?

I am assuming you are running with root? You should run these activities with oracle user.