Introduction
When a user runs a sequence of commands in Linux, the commands execute either one after the other or concurrently. However, sometimes, it is necessary to postpone the execution of commands and provide enough time for the system to produce the expected results.
In this tutorial, you will learn how to use the Linux sleep
command to delay command execution in the terminal and shell scripts.
Prerequisites
- A system running Linux.
- Access to the terminal.
What Does the Linux sleep Command Do?
The sleep
command suspends the next command calling process for a specified time. This tool is useful when the execution of the following command depends on the successful completion of a previous command.
It accepts time intervals in seconds, minutes, hours, or days, allowing flexible delays. By integrating sleep
into scripts, users control the timing of automated tasks and manage system resources.
Linux sleep Command Syntax
The sleep
command syntax is:
sleep [number]
For example, to delay the second command prompt for five seconds, enter:
sleep 5
By default, the system reads the number after sleep
as the number of seconds.
Linux sleep Command Options
Linux sleep command only has two options:
-h
,--help
. Displays the help message with usage instructions and exits.-v
,--version
. Outputs the command version information and exits.
However, it supports time units such as:
s
. Seconds.m
. Minutes.h
. Hours.d
. Days.
To specify time units, use the following syntax:
sleep [number][unit]
For example, to specify the delay time of two minutes and five seconds, run:
sleep 2m 5s
Additionally, set multiple durations by providing them as separate arguments, which are added together to determine the total delay.
The sleep
command also supports floating-point numbers, which include a decimal point (e.g., 1.5 or 0.25).
To stop sleep
after it starts and before the specified waiting period ends, press Ctrl + C
.
Linux sleep Command Examples
The sleep
command is used to control the timing of automated tasks, manage delays between commands, or ensure sufficient time for a process to complete before proceeding with the next operation.
The following sections contain examples of uses of the sleep
command in the terminal or shell scripts.
Note: The sleep
command is designed to work in combination with other Linux commands. For a list of available Linux commands, download our free Linux Commands Cheat Sheet.
Set Up an Alarm
Use sleep
to schedule the system to play an mp3 file after a specified time. In this example, the command uses mplayer to play alarm.mp3 after a seven-hour and 30-minute delay:
sleep 7h 30m && mplayer alarm.mp3
After the specified time, the system plays the file:
Delay Commands in Terminal
sleep
is useful for enforcing a time between the execution of two commands. The following example makes echo commands execute in one-second intervals:
sleep 1 && echo "one" && sleep 1 && echo "two"
Assign a Variable to the sleep Command
It is possible to assign a variable to specify the sleep
command duration. To do that, create an example shell script. Take the following steps:
1. Use a text editor like Vim to create a new script file. For example, type the following command:
vim sleep_script.sh
2. Copy and paste the following script:
#!/bin/bash
SLEEP_INTERVAL="30"
CURRENT_TIME=$(date +"%T")
echo "Time before sleep: ${CURRENT_TIME}"
echo "Sleeping for ${SLEEP_INTERVAL} seconds"
sleep ${SLEEP_INTERVAL}
CURRENT_TIME=$(date +"%T")
echo "Time after sleep: ${CURRENT_TIME}"
In this script, the variable SLEEP_INTERVAL
has the value 30, which is then used as an argument for the sleep
command. The script calculates and displays the time before and after sleep, showing that the execution has been paused for 30 seconds.
3. Save and exit the file.
4. Run chmod to make the script executable:
chmod +x sleep_script.sh
The command has no output.
5. Execute the script with:
./sleep_script.sh
When you run the script, it displays the time before the sleep
command. After pausing for 30 seconds, it shows the time after the sleep
command, displaying the delay.
Define Check Intervals
The following example illustrates the use of the sleep
command in a script that checks whether a website is online. The script continuously attempts to ping the website, and if the ping is successful, it stops. If the ping fails, the script waits 10 seconds before trying again.
To accomplish that, take the following steps:
1. Create a new script file in a text editor of choice:
vim check_website.sh
2. Paste the following content:
#!/bin/bash
while :
do
if ping -c 1 www.google.com &> /dev/null
then
echo "Google is online"
break
fi
sleep 10
done
The script continuously pings www.google.com until it receives a successful response. If the ping is successful, it prints Google is online
and exits the loop. If the ping fails, it waits 10 seconds before retrying.
3. Save the file and exit.
4. Run the following command to make the script executable:
chmod +x check_website.sh
The command has no output.
5. Execute the script with:
./check_website.sh
Allow Time for Operation Completion
Use sleep
to manage a bash script that internally calls two other bash scripts – one that runs tests in the background and another that prints the results. To ensure the second script doesn't print incorrect results before the first script has finished, run sleep
to introduce a delay between checks.
Take the following steps to do that:
1. Create a script in a text editor:
vim wait_for_process.sh
2. Paste the following content:
while kill -0 $BACK_PID ; do
echo "Waiting for the process to end"
sleep 1
done
Note: Replace $BACK_PID
with the actual process ID of the background process you want to monitor for the script to work.
The script uses the kill -0
command to check if the process with the ID stored in $BACK_PID
is still running. If the process is running, it prints a message, sleeps for one second, and checks again until the process finishes.
3. Save and exit the file.
4. Make the script executable with:
chmod +x wait_for_process.sh
The command has no output.
5. Run the script with:
./wait_for_process.sh
The output is the following:
"Waiting for the process to end"
repeatedly if the process is running."Process finished"
if the process finishes."Process not found!"
if the process finishes.
Predict Latency
Use sleep
to allow latency of certain command executions. The script snippet below shows how sleep
gives the CPU enough time to perform the calculation before the next iteration.
Take the following steps:
1. Create a script file with a text editor:
vim predict_latency.sh
2. Paste the following content:
for (( i = 1 ; i <= 250 ; i++ ));
do
sleep 1
qsub computation"${i}".pbs
done
The script loops 250 times, increasing i
(a variable used as the loop counter) by one each time. It waits one second between each loop and submits a job with a file name based on i
.
3. Save and exit the file.
4. Make the script executable with:
chmod +x predict_latency.sh
The command has no output.
5. Run the script with:
./predict_latency.sh
If everything works correctly, there is no output until the job scheduler processes the jobs. After submitting all 250 jobs, the script completes silently, with a one-second delay between each submission.
Conclusion
This tutorial explained how to use the Linux sleep
command to pause the execution of the commands in a sequence.
To learn how to wait for background running processes to complete and return the exit status, read about the bash wait command.