Sleep command in Ubuntu
Commands

Using Sleep Command in Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

If you're looking for a way by which you can delay the execution of commands and scripts, the sleep command will do the job flawlessly!

But before jumping to the examples part, let's have a look at some basics.

Practical Examples of Sleep command

The sleep command allows users to delay the execution of commands and scripts from seconds (default) to minutes and even days!

So let me start with the easy syntax of the sleep command:

sleep [TIME][suffix]

Where,

[TIME] is where you've to specify the time in the numbers of how long you want the delay.

at [suffix], is where you'll be specifying the unit of time such as:

  • s for seconds.
  • m for minutes.
  • h for hours.
  • and d for days.

And you can always use Ctrl + c to eliminate the effect of the sleep command if no longer needed.

Now, let's have a look at some practical examples of sleep commands.

1. Delay Command Execution

Pairing sleep command with others involves no complexity and can easily be done using &&. Let me show you how.

For this example, I'll be using the echo command two times to get the current time and use sleep command in between so you can have a better idea:

echo the current time is $(date +"%r") && sleep 10 && echo and the time after pause of 10 seconds is $(date +"%r")
use sleep command to delay command execution

2. Setup Alarm using sleep Command

This is another example of how you can delay the execution of various utilities.

And in this example, I'll execute the utility called mplayer after certain hours that would work as an alarm.

sleep 10s && mplayer wakeup_alarm.mp3
use sleep command in ubuntu to make alarm

You can even use this for hours making it work as a real alarm!

Customer Referral Landing Page - $100
Cut Your Cloud Bills in Half Deploy more with Linux virtual machines, global infrastructure, and simple pricing. No surprise bills, no lock-in, and the
Get started on Linode with a $100, 60-day credit for new users.

3. Pause Execution of Script

This is the most practical use case of the sleep command and to make it easy, I'll be using a simple script to show you how you can delay the execution.

The script which I will be running is:

SLEEP_TIME="10"
echo "Current time: $(date +%r)"
echo "Hi, I'm sleeping for ${SLEEP_TIME} seconds ..."
sleep ${SLEEP_TIME}
echo "All done and current time: $(date +%r)"

Here, I used sleep commands between the execution of two echo commands to make it easy.

delay shell script execution in linux using sleep command

4. Define Intervals in Scripts

The above command used the sleep command for once but you can define multiple intervals using loops.

So how about a while loop that will exit after getting a successful ping and will add a delay of 10 seconds between unsuccessful pings?

Here you have it:

#!/bin/bash
while :
    do
        if ping -c 1 www.itsfoss.com &> /dev/null
        then
        echo "itsfoss is online"
        break
        fi
    sleep 10
done
add multiple delays using sleep command in ubuntu

Well, this was just a single loop and you can be creative, especially with heavy workloads!

Final Words

The sleep command does not have many options and has a single problem to solve: adding delays in between executions.

And with enough skills, you can do wonders out of it! Let me know if you're struggling with any queries.