Time condition exit loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Time condition exit loop
# 1  
Old 04-01-2013
Time condition exit loop

Hi All,

Requirement: The below script should automatically exit at 6pm everyday without manually killing the script

Tried running with the below shell script but found the script was still running when the time was 6:15pm. The script did not exit the while loop at 6pm

The script runs thorugh cron at 9AM Mon-Fri

Code:
Script xyz.sh

#!/bin/bash

hr=$(date +"%H")
while [ $hr -ge 9 -a $hr -lt 18 ]; do
some task is performed...
done

Thanks for your time!

Regards,
a1_win

Last edited by Corona688; 04-02-2013 at 02:42 PM..
# 2  
Old 04-02-2013
Try this:
Code:
hr=$(date +"%H")
while [ $hr -ge 9 -a $hr -lt 18 ]
do
  some task is performed...
  sleep 5 # 5 secs; So you don't have a tight loop
  hr=$(date +"%H")
done

# 3  
Old 04-02-2013
Hi Spacebar,

Thanks but this is not working!

Tried executing with the below command but it exited without running anything..

Code:
hr=$(date +"%H")
while [ $hr -ge 22 -a $hr -lt 01 ] # For example,tried time range between 22hrs and 01 AM as test 
do
   do some task....
  sleep 5 
  hr=$(date +"%H")
done

output:

Code:
-bash-3.2$ ./xyz.sh &
[1] 30061
-bash-3.2$
[1]+  Done                    ./xyz.sh
-bash-3.2$

Any other suggestion?


a1_win
# 4  
Old 04-02-2013
Quote:
Originally Posted by a1_win
Hi Spacebar,

Thanks but this is not working!

Tried executing with the below command but it exited without running anything..

Code:
hr=$(date +"%H")
while [ $hr -ge 22 -a $hr -lt 01 ] # For example,tried time range between 22hrs and 01 AM as test 
do
   do some task....
  sleep 5 
  hr=$(date +"%H")
done

output:

Code:
-bash-3.2$ ./xyz.sh &
[1] 30061
-bash-3.2$
[1]+  Done                    ./xyz.sh
-bash-3.2$

Any other suggestion?


a1_win
# 5  
Old 04-02-2013
There is no number that is greater or equal than 22 and at the same time lower than 1.
Code:
hr=$(date +"%H")
while [ $hr -ge 22 -o $hr -lt 01 ] # For example,tried time range between 22hrs and 01 AM as test 
do
   do some task....
  sleep 5 
  hr=$(date +"%H")
done

# 6  
Old 04-02-2013
Linux Time script

Hi a1_win,

Below script will execute in infinite while loop. When current time (cur_time) reaches script kill time (kill_time) it will exit the script.
Code:
#!/bin/bash

kill_time=1800
while true
do
          cur_time=`date "+%H%M"`
          if [ $cur_time -gt $kill_time ]; then
                    exit 5           # To find exit status of the script
          fi
          echo "Hello World"
          sleep 60                 # To execute script every 1 minute
done


Last edited by Franklin52; 04-02-2013 at 08:44 AM.. Reason: Please use code tags
# 7  
Old 04-02-2013
Quote:
Originally Posted by a1_win
Thanks but this is not working!
Tried executing with the below command but it exited without running anything..
Code:
hr=$(date +"%H")
while [ $hr -ge 22 -a $hr -lt 01 ] # For example,tried time range between 22hrs and 01 AM as test 
do
   do some task....
  sleep 5 
  hr=$(date +"%H")
done

The script doesn't understand "time" it is just seeing integers, so make sure your comparison numerical wise is valid.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Make expect exit the UNIX script in erreneous condition

Hi, I am writing a menu driven program using shell script. THe script will be collecting data by logging into the other servers and bringing back the data to home server to process it and accordingly issue commands. TO automate commands execution , I am using expect script. However I am not able... (5 Replies)
Discussion started by: ashima jain
5 Replies

2. Shell Programming and Scripting

How to exit from shell script if above condition fails?

HI cd ${back_home} if above back_home does not exist, then script shoul exit. Please let us know how to do that (7 Replies)
Discussion started by: buzzme
7 Replies

3. Emergency UNIX and Linux Support

For loop exit

Below for loop not exiting. Can someone help? JBOSS_INST_ARGS=01 02 if ; then for i in $JBOSS_INST_ARGS; do /u/jboss-6.1.0.Final/bin/jboss_init_wise$i.sh start; done (8 Replies)
Discussion started by: vino_hymi
8 Replies

4. Shell Programming and Scripting

Strange exit of while loop

This code is used to check for duplicate ip and hostnames in an /etc/hosts file CENTRAL is path to /etc/hosts AWK =awk #check CENTRAL for duplicate ips or hostnames# grep -v "^#" $CENTRAL | $AWK '{ print $1, $2; }' | \ while read ip hostname do if... (5 Replies)
Discussion started by: trimike
5 Replies

5. Shell Programming and Scripting

Loop exit control

Hi I would like to exit the loop below on <Enter> even if it sleeps. Is it possible? while true do my_procedure; sleep 60 done Thanks (7 Replies)
Discussion started by: zam
7 Replies

6. Shell Programming and Scripting

do nothing if condition is not met but not exit

Hello all, I created the below script....and it seemed to be working fine. My problem is i want the script to ignore rest of the things if my condition is not met but do not exit.... #!/bin/ksh ########################### ########################### # Set name of the listener, this... (2 Replies)
Discussion started by: abdul.irfan2
2 Replies

7. Shell Programming and Scripting

Exit from loop

hi, how to exit from "if" loop?actually i have mutliple "if" conditions, i have to exit from each "if" loop,if it is true...:confused: Please suggest me... (3 Replies)
Discussion started by: sreelu
3 Replies

8. Shell Programming and Scripting

Exit for loop in a shell script if a condition is successfull

Hi All, I am stuch in a script where a for loop is running to execute some commands for some values. Now my problem is i have to have an if condition that if the first iteration is successful then it has to exit the for loop otherwise it has to continue normally. my code is this: for... (5 Replies)
Discussion started by: usha rao
5 Replies

9. Shell Programming and Scripting

exit out of a while loop with error

im running a while loop as a file watcher, with incremental counter on the retries..however when the retries reach it's limit i want it exit and echo and error and stop the batch. Im not sure the code i have will do that already... Here is what i have that works: #!/usr/bin/ksh count=0... (2 Replies)
Discussion started by: sigh2010
2 Replies

10. Shell Programming and Scripting

while loop exit

i wrote a while script as part of a huge program. this script, once picked, begins to output data to the person using it. pretty easy, as the person doesn't have to keep typing commands to get the output that the while loop automatically throws out. now, the thing is, while this while-script... (3 Replies)
Discussion started by: Terrible
3 Replies
Login or Register to Ask a Question