M HYPE SPLASH
// general

Unix: sleep until the specified time

By Abigail Rogers

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

4

3 Answers

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.
  • -j telling date not to actually set the date.
3

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

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.

5

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy