In this short guide, I'll show you how to sleep until a specific time or date in Linux. Command sleep can be used in combination with the date command.

Sleep Until a Specific Time

For example, to sleep until a specific time, such as 5:00 PM, we can use the following command:

sleep $(($(date -d '5:00 PM' '+%s') - $(date '+%s')))

This command calculates the number of seconds until 5:00 PM and then tells the sleep command to sleep for that number of seconds.

To understand the command better we can check the following bash example:

current_time=$(date +%s)
target_time=$(date -d '04/01/2023 12:00' +%s)

sleep_seconds=$(( $target_time - $current_time ))

sleep $sleep_seconds

Sleep Until a Specific Date

To sleep until a specific date and time, such as January 5th, 3:00 PM, you can use a command like this:

sleep $(($(date -d 'Jan 5 3:00 PM' '+%s') - $(date '+%s')))
Note:

These commands will sleep until the specified time or date in the local timezone of the system on which the command is run.

Sleep and different timezone

To sleep until a specific time or date in a different timezone, we can use the TZ environment variable in combination with the date command.

For example:

TZ='Europe/Paris' sleep $(($(date -d 'Jan 5 3:00 PM' '+%s') - $(date '+%s')))

Script will sleep until 3:00 PM on January 5th in Paris, France.

Sleep based on user input

Finally we can use the user input in order to delay/sleep Linux script. The following example show how we can do it:

#!/bin/bash
read -p "How long to delay (60 - 60m)? " answer

delay=$(echo "$answer * 60" | bc)
sleep $delay