The sleep command, as the name suggests, pauses the execution of subsequent commands or scripts for a specified period. This delay facilitates various tasks, such as scheduling commands, managing script flow, or waiting during program execution.
Syntax:
The syntax for the sleep command in Linux is:
sleep [NUMBER][SUFFIX]
- NUMBER: The number of seconds to sleep. This can be a decimal number.
- SUFFIX: An optional suffix that specifies the unit of time. The following suffixes are available:
- s: seconds (the default)
- m: minutes
- h: hours
- d: days
Sleep Command Examples:
Example 1: Pause Execution for 3 Seconds
sleep 3
This command pauses the execution of the script for 3 seconds. After 3 seconds, the script will continue execution.
Example 2: Pausing for 5 minutes
sleep 5m
This command pauses the execution of the script for 5 minutes. After 5 minutes, the script will continue execution.
Example 3: Delay Execution for 2 Hours
sleep 2h
This command pauses the execution of the script for 2 hours. After 2 hours, the script will continue execution.
Example 4: Pause for 30 Seconds and 500 Milliseconds
sleep 30.5s
Using a decimal point allows specifying a duration with seconds and fractions of a second.
Example 5: Suspend Execution for 1 Hour, 15 Minutes, and 30 Seconds
sleep 1h15m30s
This command uses different time units (‘h’ for hours, ‘m’ for minutes, and ‘s’ for seconds) to create a precise delay of 1 hour, 15 minutes, and 30 seconds.
Example 6: Pause for 2 Days
sleep 2d
The ‘sleep’ command can also be used to halt execution for multiple days by specifying ‘d’ for days as the time unit.
Example 7: sleep Command Bash Script
#!/bin/bash echo "Hi, I'm sleeping for 5 seconds..." sleep 5 echo "I'm awake Now."
This script first prints a message indicating that it’s going to sleep for 5 seconds, then pauses execution for 5 seconds using the sleep command, and finally prints a message indicating that it’s awake again.
Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.
- Check Information Technology Books
- Practice Programming MCQs
- Check Linux Books
- Apply for Programming Internship