How to Bash sleep until time or delay a specific amount of time. How do I pause for 5 seconds in a bash shell script on a Linux? Bash script the essential for DevOps Roles.
To make a Bash script sleep until a specific time, you can use the sleep
command in combination with the date
command to calculate the remaining time until the desired time is reached.
In Linux, the sleep command to add delay for a specified amount of time.
The syntax sleep command
sleep NUMBER[SUFFIX]
Where SUFFIX:
- s for seconds (the default)
- m for minutes.
- h for hours.
- d for days.
Table of Contents
Bash sleep until time
Examples
To sleep for 5 seconds
sleep 5
To sleep for 5 minutes
sleep 5m
How do I pause my bash shell script until time for 5 second
In DOS, To pause command execution until the user pressed a key. In Linux, Use the read command with the -p option
read -rsp
Explanation
- -r specifies the raw mode, which doesn’t allow combined characters like “\” or “^”.
- -s specifies the silent mode
- -p $’prompt’ specifies the prompt
- -n 1 specifies that it only needs a single character.
- -t 5 specifies a timeout of 5 seconds
Bash shell pause function
#!/bin/bash # init function pause(){ read -sp "$*" } pause 'Press [Enter] key to continue...'
The screen output terminal:
Conclusion
Thought the article, you can use Bash sleep until the time as above. I hope will this your helpful. For more details refer to Bash script.