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.
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
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?
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
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