Sleep until a specific time/date

前端 未结 18 2096
栀梦
栀梦 2020-11-28 21:13

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\"-d

18条回答
  •  清酒与你
    2020-11-28 21:36

    Use 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
    

    All done!

提交回复
热议问题