1

I'm trying to run a shell script in (#!/bin/sh in HP-UX) on exact times(hh:mm:ss) hh:05:00 .. hh:10:00 .. hh:15:00 .. hh:20:00 .. hh:25:00 and so on. I understand this could be done using cron but I'm looking for another way to do this.

Adding: watch/repeat are not supported

1 Answers1

0

You could do this:

sleep $(($(date --date="14-JAN-2015 17:00" +%s) - $(date +%s)))
run command ...

where 14-JAN-2015 17:00 is the date and time you want to wait until.

To run every five minutes, you can consider that the seconds returned by date will be a multiple of 300 on 5 minute boundaries, so this will calculate the difference between now and the next five minute boundary (returning between 0 and 299):

timeNow=$(date +%s)
sleepTime=$(( ( ( $timeNow + 299) / 300 * 300 ) - $timeNow ))
sleep $sleepTime

If date +%s is not available, try:

timeNow=$(awk 'END { print systime() }' /dev/null)

Adding: also worked with:

perl -e 'print time(), "\n" '
rghome
  • 417
  • well .. script run time is about 2minutes .. i want to wait untill time is hh:mX:ss where X is equal to (0,5,10,15,20,25,30,35,40,45,50,55) – soulseeker Jan 14 '15 at 15:59
  • Using my suggested method, you would have to generate the next start time using some date arithmetic based on the current time, which is possible, but a bit fiddly. You can do date arithmetic with date. I will see if I can think of something. – rghome Jan 14 '15 at 16:09
  • I used something similar .. assign script run time in a variable 'x' then do 'sleep 300 - x' ... with doing this and running the script at hh:mX:00 .. with X is equal to any of the values i mentioned earlier .. it works, but after a while there are delays introduced .. so it is no longer running at hh:mX:00 .. instead hh:mX:0[1,2,3] ... DELAYS – soulseeker Jan 14 '15 at 16:37
  • With my version, it subtracts the current time, so delays will not accumulate. – rghome Jan 14 '15 at 16:59
  • Exactly what I'm doing and getting delays, I'm using $SECONDS which is basically a seconds counter.. I assign it to $a at the beginning of script run and to $z at the end of the script, then do "sleep 300-(b-a)" ... getting delays accumulated over time. – soulseeker Jan 14 '15 at 17:30
  • Yes - you will get errors over time. At some point you need to anchor on the actual time by running the date command, as in the example above. – rghome Jan 14 '15 at 17:37
  • date: bad format character - s I think the date version is too old to support %s – soulseeker Jan 15 '15 at 10:24
  • Adding: also worked with: perl -e 'print time(), "\n" ' – soulseeker Jan 15 '15 at 17:25