sleep – wait for a number of seconds

Screen Shot 2019-05-15 at 10.33.57.png

sleep command is a basic Unix tool that suspends execution of your shell script or shell session for a given number of seconds. It’s a very popular mechanism for controlling flow of automation scripts in Unix and Linux.

Basic sleep Command Example

Just give a number of seconds and wait for it to return:

[email protected]:~ $ sleep 3
[email protected]:~ $

This will wait for exactly 3 seconds, then give you the shell prompt again.

sleep Command Combined with date Command

It’s much easier to demonstrate sleep command in action when you invoke date command before and after sleeping:

[email protected]:~ $ date
Wed 15 May 2019 10:25:00 IST
[email protected]:~ $ sleep 3
[email protected]:~ $ date
Wed 15 May 2019 10:25:06 IST
[email protected]:~ $

here we can see that roughly 6 seconds passed between running date commands. That’s because we’re manually typing each command.

Here’s a better way to check sleep: run all three commands automatically:

[email protected]:~ $ date; sleep 3; date
Wed 15 May 2019 10:26:17 IST
Wed 15 May 2019 10:26:20 IST
[email protected]:~ $

As you can see in the example above, there’s been exactly 3 seconds between date command outputs.

Monitoring OS resources with sleep command

Quite commonly you will run a certain command that reports immediate state of your operating system, then get another similar output later to compare progress. sleep is incredibly useful for these scenarios. We’re using while loop in bash shell for this.

Here’s how we can check current time every second (press Control+C to interrupt):

[email protected]:~ $ while true; do date; sleep 1; done
Wed 15 May 2019 10:29:03 IST
Wed 15 May 2019 10:29:04 IST
Wed 15 May 2019 10:29:05 IST
Wed 15 May 2019 10:29:06 IST
Wed 15 May 2019 10:29:07 IST
^C

And here’s how we can check average system loads using w command. ┬áThis doesn’t work that well on a 1-sec interval, but run it with interval of 1min and it’s much more useful already:

[email protected]:~ $ while true; do w; sleep 1; done
10:29 up 1 day, 11:33, 4 users, load averages: 2.09 2.44 2.62
USER TTY FROM [email protected] IDLE WHAT
greys console - Mon22 35:31 -
greys s000 - Mon22 18 /usr/bin/less -is
greys s001 - Mon22 35:31 -bash
greys s002 - 10:22 - w
10:29 up 1 day, 11:33, 4 users, load averages: 2.09 2.44 2.62
USER TTY FROM [email protected] IDLE WHAT
greys console - Mon22 35:31 -
greys s000 - Mon22 18 /usr/bin/less -is
greys s001 - Mon22 35:31 -bash
greys s002 - 10:22 - w
10:30 up 1 day, 11:33, 4 users, load averages: 2.49 2.51 2.64
USER TTY FROM [email protected] IDLE WHAT
greys console - Mon22 35:31 -
greys s000 - Mon22 18 /usr/bin/less -is
greys s001 - Mon22 35:31 -bash
greys s002 - 10:22 - w
10:30 up 1 day, 11:33, 4 users, load averages: 2.49 2.51 2.64
USER TTY FROM [email protected] IDLE WHAT
greys console - Mon22 35:31 -
greys s000 - Mon22 18 /usr/bin/less -is
greys s001 - Mon22 35:31 -bash
greys s002 - 10:22 - w
10:30 up 1 day, 11:33, 4 users, load averages: 2.49 2.51 2.64
USER TTY FROM [email protected] IDLE WHAT
greys console - Mon22 35:31 -
greys s000 - Mon22 18 /usr/bin/less -is
greys s001 - Mon22 35:31 -bash
greys s002 - 10:22 - w
^C

See Also