sleep - wait for a number of seconds
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:
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:
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:
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):
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:
See Also
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:
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:
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:
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):
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: