Looping and waiting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping and waiting
# 1  
Old 11-22-2006
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  
Old 11-22-2006
#!/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  
Old 11-23-2006
Hey aju_kup

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

Does anyone know how to answer my original query?

Mike
# 4  
Old 11-29-2006
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..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Some I/O requests to this LV are waiting

Hi All I have a blade BL860c running on a C7000 chassis, in which is connected to a NetApp, so lately I am having I/O issues, and dmesg as well as syslog.log is reporting the following: /dev/vg01/lvol2 file system file data error in dev/block 0/55892768 Page I/O error occurred while paging... (2 Replies)
Discussion started by: fretagi
2 Replies

2. UNIX for Dummies Questions & Answers

Waiting for a file

I am such a newbie. I am from a mainframe background, so this stuff is new for me to interpret. I dont know if I need a sleep or wait. I need a loop to wait for a filea or fileb to be created before I execute further script. The driver unix script is on the solaris. The files I'm looking for... (1 Reply)
Discussion started by: Lillyt
1 Replies

3. Shell Programming and Scripting

not waiting...why?

My shell script is not waiting. I right click on a file and say convert it to whatever and it runs this script. It converts it using Compressor but I want it to wait until it is 100% done before moving on and it is not waiting. I have tried to put it in the background and using "wait". I have tried... (10 Replies)
Discussion started by: mainegate
10 Replies

4. Shell Programming and Scripting

not waiting

I have a script that runs Compressor and converts the file then I want it to inject the file with Flvtool2. My script works fine but the flvtool2 is starting too early. I have tried to put it in the background by putting a "&" sign at the end and then put a "wait" on a new line. Anyone have any... (2 Replies)
Discussion started by: mainegate
2 Replies

5. Shell Programming and Scripting

waiting

I have a text file which contain all the parameters need for scheduled jobs, then this "control" script would be called everynight at certain time while read line do $myScript.sh $line & pid=$! i=`expr $i + 1` done < $list then I need to wait until all... (1 Reply)
Discussion started by: mpang_
1 Replies

6. UNIX for Dummies Questions & Answers

Waiting between commands

Hello, Taking my scripting one step farther, I want to run a number of commands one after the other, but the system should wait between commands. How do I do this? Do I use the /wait command, or is it a function wait()? rsync command /wait tar command /wait move to a new folder /wait... (1 Reply)
Discussion started by: patwa
1 Replies

7. UNIX for Advanced & Expert Users

Process(s) ID waiting on IO?

Hello Experts!! My CPU is waiting a lot (around 33%) on I/O. I would like to find out what process(s) are waiting on the i/o. Below is my real time output of vmstat and sar. Thanks for you help !!!! Regards Citrus OS: AIX - 5L : /u2/oracle >oslevel 5.3.0.0 : /u2/oracle... (9 Replies)
Discussion started by: Citrus143
9 Replies

8. UNIX for Advanced & Expert Users

browser waiting

hi all i am opning a browser like www.example.com i would like to open this browser slowly (wait mode) how can i do this. is there any option like wait for some time that to 5sec.,10 sec. like (1 Reply)
Discussion started by: munna_dude
1 Replies

9. Shell Programming and Scripting

Waiting on a group

In the below artificially simplified script, I want to wait till after a, b, and c have been printed, before printing "finished." If you just want to wait on a single background process, you can use $! (pid of the last background process). But this doesn't work if there's more than one... (1 Reply)
Discussion started by: tphyahoo
1 Replies

10. Linux

waiting process

how to know the information of the waiting process how to calculate the time of the process that it has taken to execute i want to make a program that Should be able to keep a log of the processes expired(The log should contain the starting time, expiry time, time slices used, total execution... (2 Replies)
Discussion started by: shukla_chanchal
2 Replies
Login or Register to Ask a Question