I want my bash script to sleep until a specific time. So, I want a command like "sleep" which takes no interval but an end time and sleeps until then.
The "at"-daemon is not a solution, as I need to block a running script until a certain date/time.
Is there such a command?
As mentioned by Outlaw Programmer, I think the solution is just to sleep for the correct number of seconds.
To do this in bash, do the following:
current_epoch=$(date +%s) target_epoch=$(date -d '01/01/2010 12:00' +%s) sleep_seconds=$(( $target_epoch - $current_epoch )) sleep $sleep_seconds
To add precision down to nanoseconds (effectively more around milliseconds) use e.g. this syntax:
current_epoch=$(date +%s.%N) target_epoch=$(date -d "20:25:00.12345" +%s.%N) sleep_seconds=$(echo "$target_epoch - $current_epoch"|bc) sleep $sleep_seconds
Note that macOS / OS X does not support precision below seconds, you would need to use
brew instead → see these instructions
sleep, but compute the time using
date. You'll want to use
date -d for this. For example, let's say you wanted to wait until next week:
expr `date -d "next week" +%s` - `date -d "now" +%s`
Just substitute "next week" with whatever date you'd like to wait for, then assign this expression to a value, and sleep for that many seconds:
startTime=$(date +%s) endTime=$(date -d "next week" +%s) timeToWait=$(($endTime- $startTime)) sleep $timeToWait