sleep Command: Tutorial & Examples
Wait for a specified amount of time
sleep is a simple yet powerful shell command in Linux. It is used to delay for a specified amount
of time during its execution, essentially pausing the command for the given period. The
sleep command is typically
used in scripts and can be very helpful when a delay is needed for synchronization purposes or to control the execution
flow of scripts.
sleep Command Works
sleep command works by making the calling process sleep either until the specified amount of time has elapsed or a
signal is delivered. It is a part of the GNU core utilities package which is installed on almost all Linux systems. The
time unit can be defined in seconds (s), minutes (m), hours (h), or days (d).
sleep command is crucial in instances where certain operations require a defined delay before execution. For
example, when running scripts that interact with the system or other scripts, it might be necessary to wait for a
resource to be available or a process to complete before proceeding.
Typical Problems Solved by
A classic problem solved by
sleep is the synchronization of different processes in a shell
script. For instance, if you have a script that needs to ensure a certain file is fully written before it starts
processing it, you might use
sleep to pause the script, giving the write operation enough time to complete.
Here are some examples that demonstrate the use of
To sleep for 5 seconds:
To sleep for 2 minutes:
To sleep for 1 hour:
Common Parameters of
sleep command accepts the following parameters:
For example, to sleep for 2 days, you would use:
sleep command does not produce any output. After the specified time has elapsed, the command execution continues
to the next line. For instance, if you run:
You will see:
(after 5 seconds)
This is a simple illustration of how
sleep can create a delay in the flow of script execution.
sleep command is a very useful utility in the Linux shell that allows you to introduce
intentional delays in your scripts. Whether you're trying to synchronize processes, wait for resources, or simply
control the flow of your script,
sleep has got you covered. Just remember to use it wisely to avoid unnecessary