Shell – How to sleep until a given date & time

dateshellshell-scriptsleeptime

I want to write a shell where given a date (day-hour-min) e.g.

10:00 AM Sun
2:30 PM Mon
...

It sleeps until that exact date & time.
I thought one way of doing it is, I could get the current date and calculate the difference?
then convert that into seconds and sleep for that time.

However, I am not too sure how you could do so using shell scripts?

#!/bin/bash

now=$(date '+%A %W %Y %X')
date =  2:30 PM Mon

difference = ??
sleep difference

Best Answer

if the user is allowed to use at command, this is the perfect use for that:

$ at 08:00 022116
at> myscript.sh
at>      <----------- ctrl-d here
job 9 at 2016-02-21 08:00

if you get a message like "user blah is not able to run at", ask the syadmin to add this user to at.allow file or remove from at.deny file, depending on how it is used in your environment.

Unless of course you wish this days long sleep happen in the middle of your script. Even then you can cut up your script into two, writing variables you want to keep, to a file and when the time comes and second part of your script executes, it can read those variables from the stored file.

Related Question