Unix: sleep until the specified time

12

6

Is there a (semi-)standard Unix command that would sleep until the time specified on its command line? In other words, I am looking for something similar to sleep that would take wakeup time rather than duration.

For example: sleeptill 05:00:00

I can code something up but would rather not re-invent the wheel if there's already something out there.

Bonus question: it would be great if it could take the timezone (as in sleeptill 05:00:00 America/New_York).

edit Due to the nature of what I am doing, I am looking for a "sleep until T" rather than "run command at T" solution.

edit For the avoidance of doubt, if I run the command at 18:00 and tell it to wake up at 17:00, I expect it to sleep for 23 hours (or, in some corner cases having to do with daylight savings time, for 22 or 24 hours.)

NPE

Posted 2010-12-15T10:24:06.920

Reputation: 1 169

Similar question on Unix.SE – palswim – 2019-04-24T18:37:00.180

You could use cron, I guess. – Bobby – 2010-12-15T10:30:42.913

Thanks, but for the intended use it really has to be sleep-like. – NPE – 2010-12-15T10:38:23.350

Answers

18

If you're on a Unix that has a decent date command and are using bash, you could use the %s format for calculating time.

sleep $(( $(date -j 1700 +%s) - $(date +%s) ))

(1700 being 5PM and -j telling date not to actually set the date)

w00t

Posted 2010-12-15T10:24:06.920

Reputation: 811

(+1) for the suggestion. If I run the command at 18:00 and tell it to wake up at 17:00, I expect it to sleep for 23 hours (or, in some corner cases having to do with daylight savings time, for 22 or 24 hours). This is where the suggestion unfortunately breaks down. [I also had to change the -j to -d to make it run on my Linux systems.] – NPE – 2010-12-15T11:38:00.693

7

@aix: On Linux (and other systems with GNU date), you can use sophisticated date specifications such as date -d 'tomorrow 17:00'.

– Gilles 'SO- stop being evil' – 2010-12-15T21:28:12.450

3@NPE, btw, one can compensate by using reminder-division: echo $(( ($(date -d "3:00" +%s) - $(date +%s) + (60*60*24)) % (60*60*24) )) (there might be only some issues around DST-changes) – hvr – 2014-04-08T07:57:47.560

12

How about this (combining w00t's and Gille's ideas into a function)?

function sleep_until {
  seconds=$(( $(date -d "$*" +%s) - $(date +%s) )) # Use $* to eliminate need for quotes
  echo "Sleeping for $seconds seconds"
  sleep $seconds
}

Usage:

sleep_until 8:24
Sleeping for 35 seconds

sleep_until '23:00'
Sleeping for 52489 seconds

sleep_until 'tomorrow 1:00'
Sleeping for 59682 seconds

Tyler Rick

Posted 2010-12-15T10:24:06.920

Reputation: 295

9

Check out the linux/unix at command. Using the 5AM example:

at 05:00 some_command

It also supports timezones:

A timezone name of GMT, UCT or ZULU (case insensitive) can follow to specify that the time is in Coordinated Universal Time. Other timezones can be specified using the TZ environment variable.

Jeremy L

Posted 2010-12-15T10:24:06.920

Reputation: 2 587

(+1) for the suggestion. Unfortunately, it doesn't fit naturally into my workflow. I have a script that runs a bunch of things in sequence, and I want to ensure that certain steps are not kicked off until certain times. This can of course be converted into something that uses cron or at, but it would be a pain to manage. This is the reason I'm looking for something sleep-like. For now, I've written my own. – NPE – 2010-12-15T11:20:49.913

Ah, so you want a blocking schedule. Why not have a state file keeping track of each step of the process? – Jeremy L – 2010-12-15T11:24:39.503

Could do, but it sounds somewhat more complicated than I think it needs to be. The steps are disparate commands, and the shell script is what orchestrates the workflow. Being able to "sleep till 5pm" in the script is all I really need. – NPE – 2010-12-15T11:28:24.133

In that case w00t's answer may be precisely what you're after. – Jeremy L – 2010-12-15T11:33:17.983

Yes, it's definitely in the right spirit. Sadly, it breaks down in some important (to me) cases and doesn't do timezones. – NPE – 2010-12-15T11:40:56.883