Sleep until a specific time/date

前端 未结 18 2143
栀梦
栀梦 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:39

    Here's something I wrote just now to synchronise multiple test clients:

    #!/usr/bin/python
    import time
    import sys
    
    now = time.time()
    mod = float(sys.argv[1])
    until = now - now % mod + mod
    print "sleeping until", until
    
    while True:
        delta = until - time.time()
        if delta <= 0:
            print "done sleeping ", time.time()
            break
        time.sleep(delta / 2)
    

    This script sleeps until next "rounded" or "sharp" time.

    A simple use case is to run ./sleep.py 10; ./test_client1.py in one terminal and ./sleep.py 10; ./test_client2.py in another.

提交回复
热议问题