LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-07-2011, 02:27 PM   #1
MAW123
LQ Newbie
 
Registered: Jan 2011
Location: Los Angeles, CA
Distribution: CentOS 5.5 x64
Posts: 7

Rep: Reputation: 0
Trouble writing ping script


Hi, I'm trying to write a failover script that accomplishes the following:

Backup server (B) pings main server (A) every 10 minutes, via a crontab entry.

If the ping of A fails, then B tries to ping A again in 120 seconds.

If that second ping from B to A fails, then it waits another 60 seconds, and then, if that one fails, too, it executes another script. If any of the three pings are successful, it is to exit gracefully and wait for crontab to initiate it again.

I based this script off one I found at http://www.cyberciti.biz/tips/simple...d-scripts.html

Code:
#!/bin/bash
#####################################################
#     This script will automatically run every	    #
#	  ten minutes to see if the main server is 		#
#	  online or offline. If it is offline, the		#
#	  failover mechanism will begin.				#
#													#
#	  Ping looks for failure						#
# 	  If found, sleep 120 ; reping					#
# 	  If found, sleep 60 ; reping					#
# 	  If found after that, execute bringupB.sh		#
#													#
#													#
#####################################################
# add ip / hostname separated by white space
HOSTS="10.10.10.10"
#
# no ping request
COUNT=1
# 
# If ping fails, try again in two minutes, and another minute after that
# If all three ping attempts fail, execute the failover script.
#
for myHost in $HOSTS
do
  count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ $count -eq 0 ]; then
		sleep 120;
do
  count=$(ping -c $myHost $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ $count -eq 0 ]; then
	sleep 60;
do
  count=$(ping -c $myHost $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ $count -eq 0 ]; then
	sh /failover/bringupB.sh;
fi
done
However, when the script is executed manually, I get this output:

Code:
'/BpingA_10minutes.sh: line 25: syntax error near unexpected token `do
'/BpingA_10minutes.sh: line 25: `do
This is maddening. Even basic if/else scripts I've found on-line give an error. I understand the concept of scripting, and I've written basic scripts, but this is my first conditional script and I'm stuck.

Please help.
 
Old 01-07-2011, 02:32 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
Code:
for myHost in $HOSTS
do
  count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
  if [ $count -eq 0 ]; then
		sleep 120;
                count=$(ping -c $myHost $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
                if [ $count -eq 0 ]; then
	               sleep 60;
                       count=$(ping -c $myHost $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
                       if [ $count -eq 0 ]; then
	               sh /failover/bringupB.sh;
                       fi
                fi
  fi
done
Maybe something like this?
 
Old 01-07-2011, 02:38 PM   #3
MAW123
LQ Newbie
 
Registered: Jan 2011
Location: Los Angeles, CA
Distribution: CentOS 5.5 x64
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by szboardstretcher View Post
Maybe something like this?
Thanks for the quick reply, but I get the same syntax error reply:
Code:
'/BpingA_10minutes.sh: line 25: syntax error near unexpected token `do
'/BpingA_10minutes.sh: line 25: `do
 
Old 01-07-2011, 02:42 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
Quote:
HOSTS="10.10.10.10"
#
# no ping request
COUNT=1
#
# If ping fails, try again in two minutes, and another minute after that
# If all three ping attempts fail, execute the failover script.
#
for myHost in $HOSTS; do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
sleep 120;
count=$(ping -c $myHost $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
sleep 60;
count=$(ping -c $myHost $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
sh /failover/bringupB.sh;
fi
fi

fi
done
Forgot the semicolon after the For statement and before the Do statement.
 
Old 01-07-2011, 02:46 PM   #5
MAW123
LQ Newbie
 
Registered: Jan 2011
Location: Los Angeles, CA
Distribution: CentOS 5.5 x64
Posts: 7

Original Poster
Rep: Reputation: 0
No luck. Similar syntax error:
Code:
'/BpingA_10minutes.sh: line 24: syntax error near unexpected token `do
'/BpingA_10minutes.sh: line 24: `for myHost in $HOSTS; do
FWIW, I'm using CentOS 5.5, and executing this script as root, with this command:
Code:
sh ./BpingA_10minutes.sh
 
Old 01-07-2011, 03:30 PM   #6
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
use /bin/bash, you can't write a script in 'bash' and then use 'sh' to run it.

Quote:
/bin/bash script.sh
or to see the behind the scenes do this:

Quote:
/bin/bash -x script.sh

Last edited by szboardstretcher; 01-07-2011 at 03:32 PM.
 
1 members found this post helpful.
Old 01-07-2011, 03:33 PM   #7
MAW123
LQ Newbie
 
Registered: Jan 2011
Location: Los Angeles, CA
Distribution: CentOS 5.5 x64
Posts: 7

Original Poster
Rep: Reputation: 0
You mean, run this command to execute the script?
Code:
/bin/bash ./BpingA_10minutes.sh
No go. Still the same syntax error near unexpected token error.
 
Old 01-07-2011, 03:41 PM   #8
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
Well for what its worth, here is the output from my Centos 5.5 box. It works on Fedora 14 as well. Also, did you copy and paste my recoded version of the script?

Code:
/bin/bash -x test.sh
+ HOSTS=10.10.10.10
+ COUNT=1
+ for myHost in '$HOSTS'
++ ping -c 1 10.10.10.10
++ awk -F, '{ print $2 }'
++ awk '{ print $1 }'
++ grep received
+ count=0
+ '[' 0 -eq 0 ']'
+ sleep 1
++ ping -c 10.10.10.10 10.10.10.10
++ awk -F, '{ print $2 }'
++ awk '{ print $1 }'
++ grep received
+ count=0
+ '[' 0 -eq 0 ']'
+ sleep 1
++ ping -c 10.10.10.10 10.10.10.10
++ awk '{ print $1 }'
++ awk -F, '{ print $2 }'
++ grep received
+ count=0
+ '[' 0 -eq 0 ']'
+ sh /failover/bringupB.sh
sh: /failover/bringupB.sh: No such file or directory
and some version info

Code:
uname -a
Linux servermine 2.6.18-194.17.1.el5 #1 SMP Wed Sep 29 12:50:31 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

cat /etc/redhat-release 
CentOS release 5.5 (Final)

rpm -qa |grep bash
bash-3.2-24.el5

Last edited by szboardstretcher; 01-07-2011 at 03:43 PM.
 
1 members found this post helpful.
Old 01-07-2011, 03:47 PM   #9
MAW123
LQ Newbie
 
Registered: Jan 2011
Location: Los Angeles, CA
Distribution: CentOS 5.5 x64
Posts: 7

Original Poster
Rep: Reputation: 0
Thank you so much for your help!

Running the script with +x in the execute command pointed me to encoding problems in the script, so I ran dos2unix on it (I wrote it on my Win7 host.... Grr....) and it seems to work perfectly now.
 
Old 01-07-2011, 03:49 PM   #10
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
Quote:
Originally Posted by MAW123 View Post
Thank you so much for your help!

Running the script with +x in the execute command pointed me to encoding problems in the script, so I ran dos2unix on it (I wrote it on my Win7 host.... Grr....) and it seems to work perfectly now.
Glad to help.

Go ahead and click "Yes you found this helpful" on one of these in the bottom right hand corner, to help my reputation go up. Appreciate it. And click [Solved] up top to mark the thread as solved.

Also, on my recoded version, I kept the #!/bin/bash -x in there for troubleshooting, you might want to take that -x out of there.
 
1 members found this post helpful.
Old 01-12-2011, 12:28 PM   #11
MAW123
LQ Newbie
 
Registered: Jan 2011
Location: Los Angeles, CA
Distribution: CentOS 5.5 x64
Posts: 7

Original Poster
Rep: Reputation: 0
One more thing....

Thanks for all of your help, I hope you, or someone else, can help me with one last bit.

I'm trying to have the cron job touch a file when everything is okay (in other words, when the ping is successful). I'm trying to do so via the else part of the script, but it doesn't seem to work. Here's my code:
Code:
#!/bin/bash
##############################################
#     This script will automatically run every
#     ten minutes to see if the main server is
#     online or offline. If it is offline, the
#     failover mechanism will begin.
#						    
#     Ping looks for failure
#     If found, sleep 120 ; reping
#     If found, sleep 60 ; reping
#     If found after that, execute bringupB.sh
#						   
# add ip / hostname separated by white space
HOSTS="10.10.10.10"
#
# no ping request
COUNT=1
# 
# If ping fails, try again in two minutes, and another minute after that
# If all three ping attempts fail, execute the failover script.
#
for myHost in $HOSTS; do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
/bin/touch /failover/status;
/bin/echo "Uh-oh! The ping failed. Waiting two minutes before we try again..." > /failover/status;
sleep 120;
count=$(ping -c $myHost $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
/bin/echo "Hmm... It's still not responding. Let's wait one more minute before we panic..." >> /failover/status;
sleep 60;
count=$(ping -c $myHost $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
/bin/echo "Oh, noes! It's time to bring this server online!" >> /failover/status;
/bin/bash /failover/bringupB.sh;
else
/bin/touch /failover/checked_ok;
/bin/echo "Everything is okay at:" >> /failover/checked_ok;
/bin/date >> /failover/checked_ok
fi
/bin/touch /failover/checked_ok;
/bin/echo "Everything is okay at:" >> /failover/checked_ok;
/bin/date >> /failover/checked_ok
fi
/bin/touch /failover/checked_ok;
/bin/echo "Everything is okay at:" >> /failover/checked_ok;
/bin/date >> /failover/checked_ok
fi
done
The output, when I run it manually, is:
Code:
[root@backupserver failover]# /bin/bash -x /failover/BpingA_10minutes.sh 
+ HOSTS=10.10.10.10
+ COUNT=1
+ for myHost in '$HOSTS'
++ ping -c 1 10.10.10.10
++ grep received
++ awk -F, '{ print $2 }'
++ awk '{ print $1 }'
+ count=1
+ '[' 1 -eq 0 ']'
Touching, and writing the /failover/status file works, when the ping fails, but touching/writing the /failover/checked_ok file does not work when the ping is successful.

Thanks in advance!
 
Old 01-12-2011, 12:40 PM   #12
MAW123
LQ Newbie
 
Registered: Jan 2011
Location: Los Angeles, CA
Distribution: CentOS 5.5 x64
Posts: 7

Original Poster
Rep: Reputation: 0
Nevermind, I figured it out.

Code:
#!/bin/bash
##############################################
#     This script will automatically run every
#	  ten minutes to see if the main server is
#	  online or offline. If it is offline, the
#	  failover mechanism will begin.
#						    
#	  Ping looks for failure
# 	  If found, sleep 120 ; reping
# 	  If found, sleep 60 ; reping
# 	  If found after that, execute bringupB.sh
#						   
# add ip / hostname separated by white space
HOSTS="10.10.10.10"
#
# no ping request
COUNT=1
# 
# If ping fails, try again in two minutes, and another minute after that
# If all three ping attempts fail, execute the failover script.
#
for myHost in $HOSTS; do
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
/bin/touch /failover/status;
/bin/echo "Uh-oh! The ping failed. Waiting two minutes before we try again..." > /failover/status;
sleep 120;
count=$(ping -c $myHost $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
/bin/echo "Hmm... It's still not responding. Let's wait one more minute before we panic..." >> /failover/status;
sleep 60;
count=$(ping -c $myHost $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
/bin/echo "Oh, noes! It's time to bring this server online!" >> /failover/status;
/bin/bash /failover/bringupB.sh;
else
/bin/touch /failover/checked_ok;
/bin/echo "Everything is okay at:" >> /failover/checked_ok;
/bin/date >> /failover/checked_ok
fi
else
/bin/touch /failover/checked_ok;
/bin/echo "Everything is okay at:" >> /failover/checked_ok;
/bin/date >> /failover/checked_ok
fi
else
/bin/touch /failover/checked_ok;
/bin/echo "Everything is okay at:" >> /failover/checked_ok;
/bin/date >> /failover/checked_ok
fi
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing a command (for loop) that would ping a Class C subnet medeiom Linux - Newbie 3 03-17-2012 09:49 AM
Having trouble with writing a shell script to vi smturner1 Ubuntu 1 11-02-2009 04:20 AM
Having trouble writing to CD cillm527 Linux - Hardware 16 01-09-2006 11:08 AM
Trouble writing to drives mikeleonard Linux - Software 4 02-22-2005 08:27 AM


All times are GMT -5. The time now is 02:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration