Script to restart a process | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Script to restart a process

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-08-2011
Siddheshk Siddheshk is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 31 January 2014, 12:44 AM EST
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
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.


Code:
#!/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
Sponsored Links
    #2  
Old 12-08-2011
Kesavan's Avatar
Kesavan Kesavan is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 30 March 2014, 6:58 AM EDT
Location: TamilNadu,India
Posts: 56
Thanks: 5
Thanked 1 Time in 1 Post
MySQL

you need to change the below statement


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

to


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

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

Last edited by Kesavan; 12-08-2011 at 01:38 AM.. Reason: Code Tags
Sponsored Links
    #3  
Old 12-08-2011
Siddheshk Siddheshk is offline
Registered User
 
Join Date: Jun 2009
Last Activity: 31 January 2014, 12:44 AM EST
Posts: 117
Thanks: 0
Thanked 0 Times in 0 Posts
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
    #4  
Old 12-10-2011
Sekullos Sekullos is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 21 November 2012, 4:30 PM EST
Posts: 25
Thanks: 10
Thanked 0 Times in 0 Posts
I might be wrong but I see a flaw in your contrôl.

Your contrôl is based on this check :

Code:
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


Code:
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.

Last edited by Sekullos; 12-10-2011 at 06:55 AM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to keep process running after apache restart. valiadi Web Programming 6 09-04-2010 07:51 PM
restart process based on file e-l-diablo Shell Programming and Scripting 11 08-12-2009 10:19 AM
Restart process brendan76 SuSE 3 02-26-2008 11:43 AM
need help to write script to check the process health and automatically restart it dragondad Shell Programming and Scripting 1 10-31-2006 05:16 PM
suspend/restart a process in shell script daneensign Shell Programming and Scripting 1 02-13-2006 11:43 PM



All times are GMT -4. The time now is 01:36 AM.