Shell sleep until next full minute

shellsleep

To execute a script on the next full minute I want to tell the sleep command to sleep until the next full minute. How can I do this?

Best Answer

Ask for the date in seconds: date +%s and calculate the reminder of the devision with 60 (modulo: %). If you calculate 60 minus the modulo you get the remaining seconds to the next full minute. You could change this to wait until the next full hour (change 60 to 3600).

sleep $((60 - $(date +%s) % 60)) &&
<yourscript>

To just sleep until the next full minute you can even make it shorter (without the modulo):

sleep $((60 - $(date +%S) )) &&
<yourscript>

Also be aware of this question and answer: sleep until next occurence of specific time.

Related Question