Skip to content

Instantly share code, notes, and snippets.

@anon-hui
Last active July 10, 2025 08:24
Show Gist options
  • Select an option

  • Save anon-hui/b347e047643dd2200bf01fd185e47a8b to your computer and use it in GitHub Desktop.

Select an option

Save anon-hui/b347e047643dd2200bf01fd185e47a8b to your computer and use it in GitHub Desktop.
Wait until specified time has been reached. Time format is exactly the same as the format accepted by "date -d time". (bash shell) (see example in comment) Usage: $ . wait_until.sh time
##set -- "$(( `date -d "$1" +%s`-`date +%s` ))"
#set -- "$(( `date -d "$1" +%s`-`date +%s` ))" "$2"
#[[ "$2" == "" ]] || set -- "$(( $1+$2*60 ))"
##echo "$1"
#set -- "$(( $1>=0 ? $1 : 0 ))"
#. /dev/null
##echo "$1"
#sleep -- "$1"
# * the above not work after suspend or hibernate
#set -- "$(( `date -d "$1" +%s`-`date +%s` ))" "$2"
#set -- "`date -d "$1" +%s`" "$2"
# * when system running into low resource, execution of date will error like unable to fork, so retry until success
set -- "`until date -d "$1" +%s ; do sleep 5 ; done`" "$2"
#[[ "$2" == "" ]] || set -- "$(( $1+$2*60 ))"
#set -- "$(( $1>=0 ? $1 : 0 ))"
# * need this to make "$@" be local to this script when use the script via sourcing like ". wait_until.sh ...",
# this will avoid "$@" of the caller being overwritten
# ** sourcing consume less resources and less overhead than executing the script
. /dev/null
#sleep -- "$1"
# * '10:00' --> '09:59:55'
#while (( "$1"-5 > "`date +%s`" )) ; do sleep 5 ; done
##while (( "$1" > "`date +%s`" )) ; do sleep 5 ; done
#while (( "$1" > "`date +%s`" )) ; do sleep 1 ; done
# * use "until" instead of "while" to retry execute date when low resource
# ** as both (( "" >= "0" )) and (( "" < "0" )) is false
until (( "`date +%s`" >= "$1"-5 )) ; do sleep 5 ; done
#until (( "`date +%s`" >= "$1" )) ; do sleep 5 ; done
until (( "`date +%s`" >= "$1" )) ; do sleep 1 ; done
@anon-hui
Copy link
Author

It is designed to support continued operation after resuming from hibernate.
To ensure correct behavior after hibernation, the script needs to poll every 5 seconds, and during the last 5 seconds, it will poll every 1 second.
Although it doesn't ensure 100% correct behavior, the maximum error is no more than +5 seconds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment