Sleep command

I need help in script.

I want my one script execute every time at 6:30 am and i have no cron access.

So i am putting sleep command there , Script may took half an hour 35 min , it depend upon queries how much it take time, but that is not issue,

So i want according to stop time of script, sleep time automatically adjust and it will sleep till next morning 6:30 . So please let me know how i can calculate sleep variable time.

Hi.

See man at. Creating a script with at allows you to schedule a job at a specific time. In the script when the job finishes, schedule the next execution.

Experiment with, say, 5 minute intervals to see how it might work for a daily execution ... cheers, drl

Hi Drl,

i can not use at, cron facility. that is why i use sleep command

If you have the GNU date command, try

sleep $(($(date +%s -d"6:30 tomorrow") - $(date +%s)))

Hi,

I have no GNU command

---------- Post updated at 11:29 AM ---------- Previous update was at 11:19 AM ----------

and i have aix system and it did not work there

Do you have perl?

sorry dear but i have also not perl, is there not any variable calucaltion method

If you don't have the GNU date command, you could always have a few loops:-

# Waiting until 06:30
until [ `date +%H` -le 6 ]          # Wait until after midnight and before 06:00
do
   sleep 3600                       # Sleep for an hour
done

until [ `date +%H` -eq 6 ]          # Wait until it's after 06:00
do
   sleep 1800                       # Sleep for 30 minutes
done

until [ `date +%M` -ge 24 ]         # Wait until it's after 06:24
do
   sleep 60                         # Sleep for 10 minutes
done

until [ `date +%M` -eq 30 ]         # Wait until we hit 06:30
do
   sleep 1                          # Just sleep one second now for shortest interval
done

# Run main code here

I've set several loops to reduce the number of cycles this goes through. Of course, you could just say wait until after midnight and then in a loop with a 1 second sleep, wait until we get to 06:30, but there's needless processing there. Feel free to adjust this as you see fit, but be careful you don't overstep by having a big jump too close to the required time.

How do you propose to keep this going day after day? What if there is an unexpected exit or server boot?

Robin

S=$(date +"%H:%M:%S" | BEGIN {
        # Where time digits wrap.
        split("24 60 60", T);
}
{
        split($1, A, ":")
        split(TIME, B, ":")

        # Subtract group by group like longhand arithmetic.
        for(N=3; N>=1; N--)
        {
                B[N] -= A[N]
                M=N

                while(M>0 && (B[M] < 0))
                {
                        B[M] += T[M]
                        B[M-1] --;
                        M--;
                }
        }

        print B[3]+(B[2]*60)+(B[3]*60*60)
}' )

sleep $SECONDS

Hi Corona,

U mentioned sleep $SECONDS , but u did not declare this variable in above, what is that, can you please explain me this code more

Sorry, simple mistake, sleep $S

appreciate if you can explain me this little bit

---------- Post updated at 01:26 PM ---------- Previous update was at 01:26 PM ----------

or is there any way we can store current time and we can subtract tommoorw time but i am stuck how it is

I guess Corona's proposal needs some more attention. Try adding the awk command itself, a TIME variable, and use (B[1]*6060) instead of (B[3]*6060):

S=$(date +"%H:%M:%S" | awk 'BEGIN {
        # Where time digits wrap.
        split("24 60 60", T);
}
{
        split($1, A, ":")
        split(TIME, B, ":")

        # Subtract group by group like longhand arithmetic.
        for(N=3; N>=1; N--)
        {
                B[N] -= A[N]
                M=N

                while(M>0 && (B[M] < 0))
                {
                        B[M] += T[M]
                        B[M-1] --;
                        M--;
                }
        }

        print B[3]+(B[2]*60)+(B[1]*60*60)
}' TIME="06:30:00"); echo $S
35316

will yield identical result as

echo $(($(date +%s -d"6:30 tomorrow") - $(date +%s)))
35316

HI Rudic,

Appreciate, if you just little bit explain me this program.

---------- Post updated at 04:46 PM ---------- Previous update was at 02:52 PM ----------

HI Rudic,

i trying to understand below code,and also put commnet also can you help me to understand this code where i am stuck

=$(date +"%H:%M:%S" | awk 'BEGIN {
        # Where time digits wrap.
        split("24 60 60", T);     ( this commns is splitting string 24 60 60 into T, means t become arrays of 3 value which are 24 60 60)
}
{
        split($1, A, ":")               ( not getting this command)
        split(TIME, B, ":")         ( this command splitting time = 06 30 00 in name of B array)

        # Subtract group by group like longhand arithmetic.
        for(N=3; N>=1; N--)      ( now loop start and execute for 3 times)
        {
                B[N] -= A[N]     ( B[3]=00 -= A[N}
                M=N

                while(M>0 && (B[M] < 0))
                {
                        B[M] += T[M]
                        B[M-1] --;
                        M--;
                }
        }

        print B[3]+(B[2]*60)+(B[1]*60*60)
}' TIME="06:30:00"); echo $S

It's like longhand subtraction. Consider each digit separately:

 1 0 0
-0 0 9

The loop subtracts the rightmost digit first:

1 0 -9

...and borrows one digit up:

1 -1 1

...then repeats for the next digit:

0 9 1

...and stops since it's run out of negatives to borrow for.

Except instead of always borrowing 10 it borrows 60.

I'd recommend reading man awk and become acquainted with it.

=$(date +"%H:%M:%S" | awk 'BEGIN {  
        # Where time digits wrap.
        split("24 60 60", T);                   ( this commns is splitting string 24 60 60 into T, means t become arrays of 3 value which are 
}
{
        split($1, A, ":")                       ( $1 is the first field in the input line, i.e. the actual time from date +"%H:%M:%S" )
        split(TIME, B, ":")                     ( this command splitting time = 06 30 00 in name of B array)

        # Subtract group by group like longhand arithmetic.
        for(N=3; N>=1; N--)                     ( now loop start and execute for 3 times; 1. loop: seconds;2.: minutes, 3.: hours )
        {
                B[N] -= A[N]                    ( B[3]=00 -= A[N} )                
                M=N

                while(M>0 && (B[M] < 0))        ( handle carry over as explained by Corona688 )
                {
                        B[M] += T[M]
                        B[M-1] --;  
                        M--;
                }
        }

        print B[3]+(B[2]*60)+(B[1]*60*60)       ( print result's seconds + minutes * 60 + hours * 3600 )
}' TIME="06:30:00"); echo $S