Sleep Until Query


 
Thread Tools Search this Thread
# 1  
CPU & Memory Sleep Until Query

Hi,
I am a newbie in UNIX

please help me develop code for below logic:--

I want my script to implement sleep until a specified time(date doesn't matter)

----------------------------------------
While currenttime > 14:00 and < 18:00
sleep
end while

run command

exit
-----------------------------------------
basically i want my script to run if it is not between 2PM and 6PM.
Else command should run when ever invoked.

Appreciate all help.

Regards,
Kit
# 2  
Hint:
Look at this command to find the current time in 24-hour clock format with leading zeros on the numbers:
Code:
date +%R

# 3  
Quote:
Originally Posted by methyl
Hint:
Look at this command to find the current time in 24-hour clock format with leading zeros on the numbers:
Code:
date +%R

Hi Methy,
thanks for the quick response.

Lets assume i assign x=echo `date +%R`
and it populates x=1:32

How do I compare that value with a variable containing value for 02:00PM?
I mean can i say x > y
where x=1:32 and y=02:00

Also how do i get x=13:32 instead of 1:32 by using date + %R
# 4  
Hi kiitmale,

Code:
man date

lists all available date format specifications;

in your case
Code:
date '+%H:%M'

should do the trick (but read man date as the implementation of date on a commercial unix system may be different).

The variable that stores the date may be assigned just as:
Code:
x=`date '+%H:%M'`

then the strings ascii comparison may be done:
Code:
if [ "${x}" \> "${y}" } ; then
   # ...something
else
   # ...something
fi

remember to escape string disequality operatpors with a backslash.

you may also see Other Comparison Operators

see ya
fra
# 5  
CPU & Memory my attempt till now

hi,
I tried as below:


Code:
#!/bin/ksh
##check current time
curr_time=`date +%H%M`
# SET and Initialize Time variables
time7pm=1600
time0630am=1620  ##assume currtime was 1615
echo $curr_time
echo $time7pm
while [ $curr_time -ge $time7pm and $curr_time -le $time630am ]
do
                curr_time=`date +%H%M`
                echo $curr_time
                sleep 10
done
curr_time=`date +%H%M`
echo "outside loop"
echo $curr_time 
exit 0

Somehow code nevers escapes the while Loop.
Any pointers?

Last edited by methyl; 01-14-2012 at 06:15 PM.. Reason: please use code tags
# 6  
You might find the whole process easier if you set it up as a cron job.
Take your original script and have it process one iteration then quit.
Add a cron entry to run once a minute Monday to Friday? for 00 to 13 (the last run is 13:59) and 18 to 23
Quote:
The crontabs files consist of lines of six fields each. The
fields are separated by spaces or tabs. The first five are
integer patterns that specify the minute (0-59), hour (0-23), day
of the month (1-31), month of the year (1-12), and day of the
week (0-6, with 0=Sunday). Each of these patterns may contain:
Code:
1-59     0-13,18-23   *    *   1-5    myscript

# 7  
cannot use cron job

Quote:
Originally Posted by jgt
You might find the whole process easier if you set it up as a cron job.
Take your original script and have it process one iteration then quit.
Add a cron entry to run once a minute Monday to Friday? for 00 to 13 (the last run is 13:59) and 18 to 23
Code:
1-59     0-13,18-23   *    *   1-5    myscript

Unfortunately for my requirements i cannot use cron job.
Please let me know if you can spot any issues with my code in previous post.
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #271
Difficulty: Easy
While working at Bletchley Park, in the first week of June each year Alan Turing would get a bad attack of hay fever, and he would cycle to the office wearing a service gas mask to keep from breathing pollen. In the first week of June each year he would get a bad attack of hay fever, and he would cycle to the office wearing a service gas mask to keep from breathing the pollen.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sleep while i > 0

Hi, I have a script that runs a process at the beginning and I want to sleep/wait until this process is finished and then continue with the rest of the script. I am trying with this, but it is not working: process=`ps -ef | grep "proc_p01 -c" | grep -v grep | wc -l` if ; do sleep 10 done... (7 Replies)
Discussion started by: apenkov
7 Replies

2. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

3. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

4. Shell Programming and Scripting

Wrapping 'sleep' with my 'resleep' function (Resettable sleep)

This is a very crude attempt in Bash at something that I needed but didn't seem to find in the 'sleep' command. However, I would like to be able to do it without the need for the temp file. Please go easy on me if this is already possible in some other way: How many times have you used the... (5 Replies)
Discussion started by: deckard
5 Replies

5. HP-UX

sleep & localtime query

Hi, I work on HPUX application and i had a query regarding the sleep & localtime system call in HPUX. Here is the code that we have : const uint32_t WAKE_INTERVAL_SEC(30*60); while (true) { // Find out what time it is time(&currentTime);... (3 Replies)
Discussion started by: nsvora
3 Replies

6. Shell Programming and Scripting

add the output of a query to a variable to be used in another query

I would like to use the result of a query in another query. How do I redirect/add the output to another variable? $result = odbc_exec($connect, $query); while ($row = odbc_fetch_array($result)) { echo $row,"\n"; } odbc_close($connect); ?> This will output hostnames: host1... (0 Replies)
Discussion started by: hazno
0 Replies

7. UNIX for Dummies Questions & Answers

Sleep less than 1 second

Does anyone know a way to sleep less than 1 second? Sometimes when I write scripts that iterates a loop many times it would be nice to slow things down, but sometimes 1 second is too much. (9 Replies)
Discussion started by: bjorno
9 Replies

8. Shell Programming and Scripting

Sleep under one second

If I want a script to sleep for less than a second, would I use a decimal? In other words, if I wanted my script to sleep for 1/4 of a second, would I say, SLEEP .25 ?? (5 Replies)
Discussion started by: Scoogie
5 Replies

9. UNIX for Dummies Questions & Answers

sleep

what is the purpose of the sleep command? (5 Replies)
Discussion started by: Anna
5 Replies

Featured Tech Videos