Script to restart a process

I have written a script which checks for a file if that is being updated or not. If the files is not being updated then it will restart the process.

#!/bin/sh

DATE=`date +%Y%m%d%H%M%S`
LOG_FILE=/var/xmp/log/XMP_*
INCEPT=`ls -l $LOG_FILE |awk '{print $5}'`
PROC=`xms show pr |grep -i INTERCEPT |awk '{print $1}' |grep $(hostname)`

if [ $INCEPT -gt 0 ]; then
    echo " log files are working fine !!! "
fi

sleep 30

if [ $INCEPT -gt 0 ]; then
    echo " log files are working fine !!! "
elif
    echo " log files are not working, Please Check immeiately "

printf "*****  XMP Application Alert System  *****\n\nHost: `hostname` \n\nHost_Address: \n\nInfo: Interceptor Freezed \n\nDATE:`date` \n\n" | /bin/mail -s "Host `hostname` --INTERCEPT MANAGER is freezed, Restarting...Please Check Immeiately " siddhesh.khavnekar@mobixell.com

then #restart interceptor process

`xms restart proc $PROC`

fi

This script works fine, but i have a concern here.
LOG_FILE=/var/xmp/log/XMP_* --> Here there may be 2 fils, and i wish to check the latest file by time.

How do i get this?

-SK

you need to change the below statement

INCEPT=`ls -l $LOG_FILE |awk '{print $5}'`

to

INCEPT=`ls -ltr $LOG_FILE | tail -1 | awk '{print $5}'`

This will give you the latest updated/created file by time.

Thanks. That worked.

-SK

---------- Post updated 12-09-11 at 09:39 AM ---------- Previous update was 12-08-11 at 12:16 PM ----------

Hi,

In my above script it sends a mail alert if the process is restarted. And the same works perfectly fine. I need a small modication in the script as mentioned below

In my current live systems it anyways sends a snmp alert if a process is restarted.

Can we integrate the same logic in this script? i mean it should send an snmp alert if the process is restarted through script.

Thanks in advance.

-Siddhesh.K

I might be wrong but I see a flaw in your contr�l.

Your contr�l is based on this check :

if [ $INCEPT -gt 0 ]

$INCEPT being the value of your watched log_file. What happens if the process who feeds your log file crashs? Or if the application fucks up and don't have anything else to feed to your log_file?

Your log file will still be greater than 0 and your script won't detect it.

I was thinking of a similar script for work with the same check on a log file (ls -lrt | awk '{print $5}' )
but instead of checking the current value, I compare the new value to the previous one (just by redirecting old value to random file > old_value) and if it's equal to the previous value => restart.

Something like this

NEW_VALUE=`ls -lrt $LOG_FILE |awk '{print $5}'`
OLD_VALUE=$(cat /filesystem/scripts/old_value)

if [ $NEW_VALUE = $OLD_VALUE ]
   then
          echo "bla bla, not working"
          # restart process
   else
          echo "Log check ok, incrementing"
          $NEW_VALUE > "/filesystem/scripts/old_value"
fi

Might need a little tweaking or add a count so that it only restarts the process after 2 failed checks or something.

Just an idea anyway.