sleep - wait for a number of seconds

sleep sleep command

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:

greys@maverick:~ $ sleep 3
greys@maverick:~ $

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:

greys@maverick:~ $ date
Wed 15 May 2019 10:25:00 IST
greys@maverick:~ $ sleep 3
greys@maverick:~ $ date
Wed 15 May 2019 10:25:06 IST
greys@maverick:~ $

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:

greys@maverick:~ $ date; sleep 3; date
Wed 15 May 2019 10:26:17 IST
Wed 15 May 2019 10:26:20 IST
greys@maverick:~ $

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):

greys@maverick:~ $ 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:

greys@maverick:~ $ 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 LOGIN@ 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 LOGIN@ 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 LOGIN@ 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 LOGIN@ 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 LOGIN@ 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




Contact Me

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!
Recent Articles
03 Sep 2024

macOS Sequoia 15.0

02 Sep 2024

Video: Has This Worked Before?

24 Mar 2024

Homelab: Mac Pro 2013 32GB

Recent Tweets