Imagine that you want to launch a "console" command or script in a terminal at 03:00AM, and being able to check the output at any time. But you don't want to use cron or at, because you need to control both stdin and stdout in any moment.
Yes, it seems a strange situation but it happens :P
Instead of waiting to 03:00AM to launch the command, you can use "sleep N && your-command", like this:
Add the following to your .bashrc file:
function sleep_until {
NSECONDS=$(( $(date -d "$1" +%s) - $(date +%s) ))
if [ $NSECONDS -lt 0 ];
then
NSECONDS=$(( $(date -d "tomorrow $1" +%s) - $(date +%s) ))
fi
echo "Sleeping for $NSECONDS seconds..."
sleep $NSECONDS
}
Reload it (source .bashrc) and then you can write:
sromero@compiler ~ $ sleep_until 12:16 && echo "TEST"
Sleeping for 16 seconds...
TEST
sromero@compiler ~ $ sleep_until "tomorrow 12:16" && echo "TEST"
Sleeping for 86369 seconds...
(...)
sromero@compiler ~ $ sleep_until "tomorrow 03:00" && ./your_script.py
Sleeping for 52777 seconds...
If you don't specify a date (only hour) and given(hour:minute) < current(hour:minute), the script will use the date for the next day:
sromero@compiler ~ $ date
mié feb 27 12:28:11 CET 2013
sromero@compiler ~ $ sleep_until 12:25
Sleeping for 86207 seconds...
Now you can leave the computer on and go to sleep.
Wake up at 04:00AM, check that everything's went fine and that you don't need to relaunch the command, and go again to sleep :-).
For remote systems, don't forget to run this under screen or tmux :)
PD: Adapted from http://superuser.com/questions/222301/unix-sleep-until-the-specified-time .
No hay comentarios:
Publicar un comentario