Register
It is currently Thu Jul 24, 2014 2:04 pm

Sleep not working as expected


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Wed Jun 01, 2011 9:56 am   

Joined: Wed Jun 01, 2011 9:51 am
Posts: 2
I am writing a cron script that checks a service every minute for life. If the thing dies, the cron kick starts it. However, I am trying to get two failures before considering it dead and was trying to use sleep to achieve this... however it seems to just gloss over the sleep and go to the next step in the script.

Code:
#!/bin/bash

status=`curl -s -k https://somesite.com/heartbeat`

if [ "$status" == "alive" ]; then
   echo $"Alive!"
   exit 0
else
   echo $"We may have a problem, let me check again in 20 secs before freaking out."
   sleep 20
fi

status2=`curl -s -k https://somesite.com/heartbeat`

if  [ "$status2" == "alive" ]; then
   echo  $"Nah, we cool. Alive!"
   exit 0
else
   
   echo $"No, this is for reals! Evasive maneuvers!"
   
   /etc/init.d/cmm_prod_1 force-reload;
   
   
fi


So how can I make it pause fully for 20 seconds at that point in the script before executing the next check and if statement?


Top
 Profile  
 PostPosted: Wed Jun 01, 2011 1:00 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Hiya Bubbagump!

The way that it's written, it really should sleep for 20 seconds there. It does in my test environment. I have to make a couple of alterations for it to run in my shell though. Newer versions of bash don't like the double equal "==". I also removed the "$" from the echo lines, though I don't think that's affecting how your script runs:

Code:
#!/bin/bash

status=$(curl -s -k https://somesite.com/heartbeat)

if [ "$status" = "alive" ]; then
   echo 'Alive!'
   exit 0
else
   echo 'We may have a problem, let me check again in 20 secs before freaking out.'
   sleep 20
fi

status2=$(curl -s -k https://somesite.com/heartbeat)

if  [ "$status2" = "alive" ]; then
   echo  'Nah, we cool. Alive!'
   exit 0
else
   
   echo 'No, this is for reals! Evasive maneuvers!'
   
   #/etc/init.d/cmm_prod_1 force-reload;
   echo '<do stuff here>'
   
fi


You don't get any errors or anything, it just skips to the next 'if'?


Top
 Profile YIM  
 PostPosted: Wed Jun 01, 2011 1:17 pm   

Joined: Wed Jun 01, 2011 9:51 am
Posts: 2
Hrm, ok. I put in all the echos to see how it would behave and confirm it was sleeping... but once cron gets a hold of it it seems to run then run again completely 20 seconds later. The only time I have an issue though is when it detects a failure. Otherwise it hits the first alive and cheerfully exits as one would expect. I wonder if the init script is being dumb somehow. Let me check with the dev on that. But thanks for looking at this and the tips! I may be back.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP