The Linux sleep Command – Tutorial and Examples

The sleep command is used to delay the execution of scripts or commands in Linux (and other Unix systems). In this beginner-friendly tutorial, we’ll show you how to use the sleep command on Linux and what it does, and we’ll include helpful examples.

You can use the sleep command on any Linux distro, including Debian, Ubuntu, Linux Mint, CentOS, and more. You can even use it on a Mac or other Unix-like systems. The command is available on all Linux distros, you don’t need to install it.

In this tutorial, we’ll show you how to use the sleep command in the Terminal/shell/ssh and bash scripts. So you’ll need Terminal access to a Linux distro or SSH access to a Linux server.

Basic sleep command syntax

The basic syntax of the sleep command on Linux is:

sleep number[suffix]

The number can be any integer, and the suffix can be:

  • “s” for seconds (default)
  • “m” for minutes
  • “h” for hours
  • “d” for days

So you can pause a command for a number of seconds, minutes, hours, or days.

A most basic example is:

sleep 5m

This will pause/delay the command for 5 minutes. If you want to exit out of the sleep command mode, just use “CTRL + C”

We’ll include more detailed and useful examples below.

Run an .mp3 “alarm” using the sleep command on Linux

This is the most popular and widely used example of the sleep command on Linux. Use the sleep command to delay another program running an .mp3 file. For example, if you have VLC installed, you can use:

sleep 30m && nvlc /path/to/alarm.mp3

This will run the alarm.mp3 file after 30 minutes with the CLI version of VLC. You can replace the second part of this command with whatever media player you have installed on your system.

Open a website in Firefox after 1 hour via the terminal

You can use the sleep command along with the CLI options for Firefox to open a website after a certain amount of time.

For example, you can open our website after 1 hour by running:

sleep 1h && firefox https://linuxstans.com

1 hour after you run this command, Firefox will open up with our website.

Another similar command I often use is to open YouTube videos (most often they are a scheduled live stream). So for example, if someone schedules a stream in 2.5 hours on YouTube, I will run the sleep command with Firefox and a link to their YouTube video:

sleep 2.5h && firefox -new-tab https://www.youtube.com/watch?v=lvRJt1aNgXA

This will open that YouTube video in a new tab in Firefox after two and a half hours.

I rarely use the previous example for running an “alarm” with an .mp3 file, I often just open up a song on YouTube via Firefox to remind me to take a break from whatever I’m doing.

Run a makeshift timer with the echo and sleep commands

Another example that I sometimes use with the sleep command is running a makeshift “timer” when I’m working in the Terminal and I’m too lazy to open up an actual timer.

For example, running this command:

echo "Start" && sleep 30 && echo "Stop"

The Terminal will output the word “Start”, wait 30 seconds, and output Stop. You can run a more complex version of this example, but at that point, it may just be easier to open up a Timer app.

A more complex example would be:

echo "Start now" && sleep 10 && echo "10 seconds have passed" && sleep 10 && echo "20 seconds have passed" && sleep 40 && echo "A minute has passed"

This command will output “Start now”, then after 10 seconds it will output “10 seconds have passed”, wait 10 seconds, and output “20 seconds have passed”, wait 40 seconds and output “A minute has passed”. You can modify this command and combine the sleep and echo commands to run something useful to you.

Put the computer to sleep with the sleep command

People may associate the sleep command with putting a computer to sleep, but that’s not what it’s used for. You can still use the sleep command to put a computer to sleep:

sleep 1 && systemctl suspend

This will put your computer to sleep after 1 second.

To put your computer in Hibernate mode, run:

sleep 1 && systemctl hibernate

You can also shutdown the computer after 1 second:

sleep 1 && shutdown -h now

Run the sleep command in a bash script

You can also use the sleep command in a bash script, and it’s often used in bash scripts.

A basic example of a bash script with the sleep command is:

SLEEP_SECONDS="15"
echo "The current time is: $(date +%T)"
echo "Waiting ${SLEEP_SECONDS} seconds"
sleep ${SLEEP_SECONDS}
echo "The current time is: $(date +%T)"

The output of this bash script would be similar to:

The current time is: 14:38:28
Waiting 15 seconds
The current time is: 14:38:43

You can use the sleep command directly in a bash script, without a variable, but it’s more useful and often used with a variable that can easily be changed and used throughout the script.

You can combine all examples from this tutorial into a bash script. So you could create a timer in a bash script and run ./timer.sh whenever you want to. It’s easier than typing out the full command.

Got any other sleep command examples for Linux?

Leave a comment below. I know there are a lot of useful ways to run the sleep command.

Leave a comment

Your email address will not be published. Required fields are marked *