Linux sleep command: Syntax, options, and examples

The Linux sleep command pauses script or command executions for a specific time. It is helpful to prevent your system from running a process too soon or too frequently while still keeping it automated.
If you use bash scripts to automate intricate tasks in your Linux system, the sleep command can be a handy tool to ensure processes run smoothly.
In this article, we’ll explain what the sleep command in Linux does alongside its syntax. We’ll also provide real-world examples of Linux sleep command use to help you understand how it works.
What does the Linux sleep command do?
The sleep command in Linux pauses command or script execution for a specific time. It is helpful for several tasks, such as:
- Delaying execution – runs commands or bash scripts after the specified period. This ensures the requirements for running a process are met before its execution.
- Minimize execution frequency – adds a delay to execute commands or scripts less frequently. This reduces the system or target service’s load, which can otherwise cause performance issues.
- Retrying a process – set up a retry mechanism that automatically reruns a script or command after a while instead of quitting the process immediately.
Typically, the Linux sleep command resides inside a script to add a delay between lines. However, as we’ll demonstrate later, you can use it directly in your system’s command-line interface.
Linux sleep command syntax and options
The Linux sleep command syntax looks like this:
sleep number[suffix] [options]
The number placeholder indicates the time duration, which can be a whole number or fractions written in decimals like 1.5. You can use any number as long as it is bigger than 0.
Meanwhile, [suffix] is the time unit represented by its first letter. The sleep command supports the following:
- s – seconds
- m – minutes
- h – hours
- d – days
- inf – infinity
The suffix is optional. If you don’t specify it, the sleep command will use seconds by default.
The Linux sleep command only supports these two options:
- –help – shows the sleep command manual in your system’s command-line interface.
- –version – outputs the sleep command’s version number, which is useful for checking if the command is installed and up-to-date.
Linux sleep command examples
Let’s explore how to use the sleep command in Linux. If you want to follow along, open your system’s terminal or the browser terminal if you are a Hostinger virtual private server (VPS) user.
Using suffixes in the sleep command
Suffixes in the sleep command attach directly to the end of the numbers. For example, this command will set a delay of two hours and 30 minutes:
sleep 2h 30m
Instead of using a time unit of longer duration, you can set a number higher than 60 or 24. For example, you can use the following to set a one-day delay:
sleep 24h
If you omit the suffix, the command will automatically set a delay in seconds. For example, this will set sleep for 5 seconds:
sleep 5
Specifying fractional seconds in the sleep command
Instead of specifying time fractions using more diminutive suffixes, use decimal numbers. For instance, use this command to set a 2-hour and 30-minute delay.
sleep 2.5h
This is especially useful if you want to set a pause in an increment of less than a second. For example, this will delay the command execution by 1.5 seconds:
sleep 1.5
Using sleep in bash scripts
The sleep command’s common usage is to delay execution in shell scripts. For example, let’s create a simple script that prints the message “Hello” and “World” after a brief pause. Here’s how to do so:
- Create the bash script using this command:
sudo nano hello.sh
- Add the shebang in the first line to indicate that your file is a bash script:
#!/bin/bash
- Add the echo commands to print the message like so:
echo "Hello" echo "World"
- Add the sleep command between the two lines to set a delay like this:
echo "Hello" sleep 5 echo "World"
- Press Ctrl + X, and Y, then Enter to save the script.
- Make the script executable by changing the file permissions:
chmod +x hello.sh
- Run the script using this:
./hello.sh
While this is a basic example of using sleep in a bash script, the command’s working principle remains the same regardless of the complexity of your script.
Assigning a variable to the sleep duration
When working with a bash script, you can use a variable to dynamically set the sleep value. This is especially useful if you have multiple sleep commands and update their values often.
For example, this script has two sleep commands with explicit values, which you must change individually to update the delay duration.
echo "Hello" sleep 5m echo "World" sleep 5m echo "Linux is fun"
If you use a variable, the above script will look like the following. In this example, the sleep commands use $delay as the value, which we define as 5m:
#Define the value here delay="5m" echo "Hello" sleep "$delay" echo "World" sleep "$delay" echo "Linux is fun"
Naming variables
You can use any name for your variable as long as it starts with the $ symbol.
You can use multiple variables and values like so:
#Define the values here delay1="5m" delay2="3m" echo "Hello" sleep "$delay1" echo "World" sleep "$delay2" echo "Linux is fun"
Instead of writing the variables’ values inside the script, you can also pass them from the main shell when running the script. Note that this method will automatically assign the values to $number variables according to their order. Consider this example:
./script.sh 60 20
In the above command, we pass two values into the script: 60 and 20. By default, 60 becomes the value of $1, while 20 becomes the value of $2.
If you don’t want to use $1 or $2 as variables in your script, you can reassign them to other variables. Here’s an example:
#Define the values here delay1="$1" delay2="$2" echo "Hello" sleep "$delay1" echo "World" sleep "$delay2" Echo "Linux is fun"
This means $delay1 will be 60, while $delay2 will be 20.
Setting up an alarm with sleep
Ironically, the sleep command can be a tool for setting up a timed alert to wake you up. To do it, create a script that sends a notification and sound after a specific interval, similar to an alarm.
For this, you need the desktop version of Linux to show the notification and play the audio alert. Your alarm script might look like the following:
#!/bin/bash #Set the delay before the following commands start sleep "$1" #Send a notification notify-send "Alarm!" #Play the alert sound. Replace with the actual path paplay /path/to/audio.wav
In this script, we use the sleep command with the “$1” variable so we can easily specify the delay during the execution. For example, use this command to set the alarm for five minutes from now:
./alarm.sh 600
Delaying commands in the terminal
Chained commands are a series of commands that run in order with a single execution. They are similar to lines in a bash script, except that they run directly from the command line.
Chaining commands requires operators to set the conditions for running them. For example, the double ampersands (&&) will start the second command only after the first one successfully runs.
Here’s an example of chained commands that will print Hello and immediately follow it up with World
echo "Hello" && echo "World"
Like lines in a bash script, you can also use sleep to delay the execution of chained commands. Simply add sleep between the commands using an operator.
For instance, if you want the above command to output World 30 seconds after Hello, insert sleep like so:
echo "Hello" && sleep 30 && echo "World"
Interrupting sleep
To interrupt the sleep command, simply terminate the process by pressing Ctrl + C. This will stop the delay and the command or script execution.
For example, you run a command with a 3-minute delay like so:
sleep 3m && echo "Hello world"
If you hit Ctrl + C, the execution will stop, and you won’t get any output even after three minutes. If you rerun the above command, you must wait a full three-minute delay for the output.
Defining check intervals with sleep
If you have looping scripts for regular system checks or monitoring, you can use the Linux sleep command to set the loop interval. While you can also use a cron job to schedule a process, using sleep is better for more frequent tasks.
For example, here’s a simple script that checks a service’s status and prints a message if it is active.
#!/bin/bash while true; do if systemctl is-active --quiet apache2; then echo "Apache is running." else echo "Apache is not running." fi sleep 15 # Wait 15 seconds before checking again done
Let’s break down how this script works:
- The while-true statement will loop through the script until you explicitly stop it, creating a continuous process.
- The systemctl command checks if the Apache service is running.
- The conditional if-else statement gives different outputs based on the service status.
- The sleep command sets an interval, adding a 15-second delay before the script runs again.
Conclusion
The sleep command pauses command or script execution for a specific time. It is helpful if you want to lower the execution frequency, add a delay to your process, or set up a retry mechanism.
To use the Linux sleep command, simply add the number and time unit, like sleep 2h 30s. You can use any number bigger than zero and decimals to set fractional time.
Commonly, the sleep command resides inside a bash script to add a pause between lines. However, you can also run it directly in your system’s main shell to delay chained commands.
In a real-world scenario, you may use the sleep command to set up a notification system that will get triggered after a specific time. You can also use it inside a looped script to add delay for repetitive processes like monitoring.