Linux sleep Command Tutorial for Beginners (with Examples)

Sometimes, while working on the Linux command line, you might want to run a couple of commands one after the other, but with a certain amount of delay between them. It's here, in situations like these, the sleep command may prove to be really helpful. In this tutorial, we will quickly discuss the basics of this tool using some easy to understand examples.

But before we jump into that, it's worth mentioning that all examples here have been tested on an Ubuntu 16.04 LTS machine.

Linux Sleep command

The Sleep command is used to introduce a delay for a specific amount of time. Following is its syntax:

sleep NUMBER[SUFFIX]...
sleep OPTION

And here's how the tool's man page describes it:

Pause for NUMBER seconds. SUFFIX may be 's' for seconds (the default), 'm' for minutes, 'h' for 
hours or 'd' for days.  Unlike most implementations that require NUMBER be an integer, here NUMBER
may be an arbitrary floating point number. Given two or more arguments, pause for the amount of
time specified by the sum of their values.

Following are some Q&A-styled examples that should give you a good idea on how the Sleep command works.

Q1. How to use sleep command?

Basic usage is fairly simple - just pass a number as input to make sure sleep induces a delay of that much seconds.

For example:

sleep 5

The above command will induce a pause of 5 seconds, or in other words, when you run the command, you'll get the prompt back after 5 seconds.

Q2. How to make sleep induce larger delays?

As you'd have seen in the previous Q&A, the sleep command considers the integer passed to it as number of seconds. However, if you want, you can specify a suffix along with the integer to tell Sleep that you want it to consider the integer as number of minutes, hours, or even days.

The available suffixes are 's' (for seconds - default), 'm' (for minutes), 'h' (for hours), and 'd' (for days).

For example:

sleep 5m

This command will induce a pause of 5 minutes.

Q3. How to setup an alarm using sleep?

You may want to set up an alarm for a variety of things, including setting up work intervals, taking a nap, or even for reminding something. You can use the Sleep command to do that as well. All you need to do is to execute the sleep command with the amount of delay you want, and make sure an audio or video file starts playing immediately after that.

For example:

sleep 5h; vlc sound.mp3

The above series of commands will make sure that a sound file gets played (in vlc player) after 5 hours.

Conclusion

The Sleep command is mostly used in shell scripts, although that doesn't mean you can't use it directly on the command line. Here, we've discussed the basic usage of the tool. To know more, you can head to the command's man page.

Himanshu Arora

About Himanshu Arora

Himanshu Arora has been working on Linux since 2007. He carries professional experience in system level programming, networking protocols, and command line. In addition to HowtoForge, Himanshu's work has also been featured in some of world's other leading publications including Computerworld, IBM DeveloperWorks, and Linux Journal.

Share this page:

Suggested articles

0 Comment(s)

Add comment