Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bash Binary Check

Status
Not open for further replies.

TamedTech

IS-IT--Management
May 3, 2005
998
GB
Hello Guys,

I'm trying to write a small script that does a check to ensure an application hasnt frozen for my watchdog timer. The script needs to return a brinary 0 if all is fine, and 1 if there is a problem.

I'm not sure of the best way to check that the application hasnt frozen, but at the moment I have the application writing to a file every 10 seconds or so, so I'm thinking I can compare the current system time against the time the file was last modified, and if the tollerance exceeds 30 seconds or so then return 1.

I'm a little bit lost on how to achieve this, but Ive made a rough start on the script below.

The idea is that if the application has frozen, then it terminates the process, before restarting it. It can then retest it, if it fails this second time then it returns 1, the watchdog would then reboot the system.

Code:
#! /bin/sh

lastalive=`stat -c %Y Logs/Alive.log`
currenttime=`date +%s`

echo $lastalive
echo $currenttime

amount=$(($currenttime - $lastalive))

echo $amount

if [ "$amount" < 30 ];
then
	echo 'Application Fine'
	# Return 0 as the application appears to be running fine.
else
	echo 'Application Frozen'
	# I want to terminate the appliaction here, and then restart it, then perform the check it again, only if it fails the seconds test should it return 1.
fi

There may be better ways to do this, I'm just not sure, hence this post. The goal of the script is to check if the application has frozen, and if it has, restart it, if it wont restart properly, then reboot the system.

Thanks for any help guys,

Rob
 
A couple of comments
you talk about it being a bash script but your first line #!/bin/sh should then be #!/bin/bash (or #!/usr/bin/bash on my system)

To achieve exactly what you asked for use 'exit <return code>' i.e.
Code:
if [ "$amount" < 30 ];
then
    echo 'Application Fine'
    exit 0
else
    echo 'Application Frozen'
    exit 1
fi
However, you don't need the quotes around $amount - it's a value, not a string - and, with your formatting, you don't need the semi-colon after the if statement so we've got
Code:
if [ $amount > 30 ]
then
  echo Application fine
  exit 0
else
  echo Application frozen
  exit 1
fi


Ceci n'est pas une signature
Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top