The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
waiting mpang_ Shell Programming and Scripting 1 10-04-2007 02:52 AM
Waiting between commands patwa UNIX for Dummies Questions & Answers 1 06-16-2007 02:01 PM
Process(s) ID waiting on IO? Citrus143 UNIX for Advanced & Expert Users 9 03-29-2007 05:28 PM
Waiting on a group tphyahoo Shell Programming and Scripting 1 05-24-2006 05:19 PM
waiting process shukla_chanchal Linux 2 11-03-2005 07:00 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-22-2006
mikie mikie is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 16
Looping and waiting

Hi

Is it possible to have a script run in a loop (waiting for a change of state in the network interface), and if the loop continues for five minutes, to have it email the admin, and carry on in the loop?

Here is my loop:

Code:
#!/bin/bash

STATE=`echo "show State:/Network/Interface/bond0/Link" | scutil | grep -c "TRUE"`

while [ ! ${STATE} -eq 1 ]
do
echo "Link is inactive"
/bin/sleep 2
done

echo "Link is alive"

If this is possible, how would I achieve it?

Mike
  #2 (permalink)  
Old 11-22-2006
aju_kup aju_kup is offline
Registered User
  
 

Join Date: Jul 2006
Posts: 189
#!/bin/bash

STATE=`echo "show State:/Network/Interface/bond0/Link" | scutil | grep -c "TRUE"`

while [ ! ${STATE} -eq 1 ]
do
echo "Link is inactive"
/bin/sleep 2
STATE=`echo "show State:/Network/Interface/bond0/Link" | scutil | grep -c "TRUE"
done

echo "Link is alive"
  #3 (permalink)  
Old 11-23-2006
mikie mikie is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 16
Hey aju_kup

Thanks for pointing that error in the loop out to me I should know about things like that from previous posts.

Does anyone know how to answer my original query?

Mike
  #4 (permalink)  
Old 11-29-2006
mikie mikie is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 16
Hey all

As a follow-up, I figured it out. So, if anyone is searching for a similar solution, here is my proposed script in its entireity. It is used within OS X. Note that after 5 minutes, I actually exit the script and do not continue:


Code:
#!/bin/bash

# Script to call external monitor scripts
# This is run via Launchd

###
# Setup Email
###
C_NAME=`/usr/sbin/scutil --get ComputerName`
NOTIFY="admin@admin.com"
WARNING="There is a problem with the NIC's on the primary Moodle server."
###
# End setup Email
###

TIME="0"
# The loop is for 150 times
# 150x2(sleep time)=300s=5minutes
# If this script continues to fail
# after 5 minutes it will finish the loop
# and then email the admin with necessary info
		
while [ ${TIME} -ne 150 ]
do
        
	# Sleep for 2 seconds in between to preserve
	# processor hogging

   	/bin/sleep 2
      	
	# Check state of aggregated link and interfaces associated
	# with it. Apache will not run unitl NICs are up and running
	# so the remainder of this script will fail.
		
	if [ ! $(echo "show State:/Network/Interface/bond0/Link" | scutil \
	| grep -c "TRUE") -eq 1 ]
	then
		echo Loop ${TIME}
                echo "Interface is not active"
       	        TIME=$(( ${TIME} + 1 ))
   	else 
		echo "Interface is alive"

		# Sleep to allow Apache time to start.
		# If Apache fails after this amount of
		# time, then there is something wrong
		# and shutdown_services script will be
		# called to initiate failover
			
		/bin/sleep 30

		echo "Running monitor scripts"
			
		# Call check_apache.sh script
		APACHE="/Library/IPFailover/scripts/check_apache.sh"
			
		if [ -x ${APACHE} ]
		then
			/Library/IPFailover/scripts/check_apache.sh
		else
			echo "check_apache.sh does not exist - moving to next script"
			logger \
			"FAILOVER: check_apache.sh does not exist - moving to next script"
		fi

		# Call check_php.sh script
		PHP="/Library/IPFailover/scripts/check_php.sh"
			
		if [ -x ${PHP} ]
		then
			/Library/IPFailover/scripts/check_php.sh
		else
			echo "check_php.sh does not exist - moving to next script"
			logger \
			"FAILOVER: check_php.sh does not exist - moving to next script"
		fi

		# Call check_postgres.sh script
		POSTGRES="/Library/IPFailover/scripts/check_postgres.sh"
			
		if [ -x ${POSTGRES} ]
		then
			/Library/IPFailover/scripts/check_postgres.sh
		else
			echo "check_postgres.sh does not exist - moving to next script"
			logger \
			"FAILOVER: check_postgres.sh does not exist - moving to next script"
		fi

		# Exit the script and therefore the loop
       	        # Using "break" would only exit the loop
		# and not exit the script
       	        # We don't need to email Admins
       	        # when the script is successful
                        
		exit 0
	fi
done

echo "Finished looping"
echo "Mailing the admin"
logger "FAILOVER: Problem with Network Interfaces - mailing the admin and exiting monitor.sh script"

# Mail the System Administrator

echo From: $C_NAME - ${WARNING} > /tmp/monitor_warning.txt
echo ----- >> /tmp/monitor_warning.txt
echo "Note that there has been a bad response from the "monitor" shell script."\
>> /tmp/monitor_warning.txt
echo "This means that there is a problem with the Network Interfaces of $C_NAME providing the Moodle service." \
>> /tmp/monitor_warning.txt
echo ----- >> /tmp/monitor_warning.txt
echo "This error was written out at `date`." >> /tmp/monitor_warning.txt
echo ----- >> /tmp/monitor_warning.txt
echo "Please attend to this immediately." >> /tmp/monitor_warning.txt
cat /tmp/monitor_warning.txt | mail -s \
"From: ${C_NAME} - Primary NIC's are down" ${NOTIFY}

# Should I instigate failover here?

exit 0

Hope this is of some use to someone.

Mike

Last edited by mikie; 11-29-2006 at 05:10 AM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0