Pause a Bash Shell Script for X seconds
Bash scripts are useful for running multiple commands that you might not want to type line by line into a Bash command shell. In some cases, you might need to pause a script, perhaps to accept input from the user, or to sleep before returning the results of a command.
This tutorial will explain different ways to pause a Bash shell script.
Pause a Bash script for X seconds
Suppose you are writing a script from scratch and you decide that you want to display a welcome message to potential users, so you have to delay starting the script to let your users read the message, this will explain what your script will do before it starts running further Bash commands. sleep
The command in Bash can help you achieve this, detailed here. Its syntax is as follows:
sleep n
Where n
is the number of seconds you want it to wait. n
You can also specify smaller units, such as milliseconds, by just specifying the number as a decimal - so if you want to pause for 30 milliseconds, sleep 0.030
that should do the trick!
Remember n
that is a mandatory parameter, so sleep
running without telling how long will not fully work. n
Running without a value for will produce the following error.
user@linux:~$ sleep
sleep: missing operand
Try 'sleep --help' for more information.
If you want to wait much longer than a few seconds, sleep
it also accepts a parameter to specify the unit of time (i.e. minutes, hours, days), so if you want to wait a whole week, sleep 7d
there's a solution for you!
Here are sleep
some use cases for .
# wait 500 milliseconds
sleep 0.5
# wait 1 second
sleep 1
# wait 20 seconds
sleep 20
To display a message and wait 1 minute:
echo "Hello! This script will run in exactly a minute!"
sleep 1m
Rerun the script weekly to kill long-running Python processes.
while true
do
killall --older-than 7d python3
sleep 7d
done
Pause a Bash script for a certain period of time
In some cases, you may want to wait a variable amount of time, perhaps because you want to run a script with different delays depending on how many scripts are running. A good example is if you run the same script multiple times that may need to modify the same file, you may want a different delay for each invocation of the script to ensure that no two invocations modify the file at the same time.
In this case, you can n
make _an actual Bash-style variable $N
and set its value somewhere in the script. An example is as follows:
N=5
sleep $N
The script where you place this should sleep for 5 seconds.
If you omit $N
the initialization of , and instead specify a value when you call the script, you can use the value of defined in the shell's environment N
to provide the script with sleep
the time period.
user@linux:~$ cat test.sh
#! /bin/bash
sleep $N
user@linux:~$ export N=3
user@linux:~$ chmod +x test.sh
user@linux:~$ ./test.sh
As a result, you should see the script running for 3 seconds. You can even override the environment value and N=2 ./test.sh
specify it in the script call by executing
You might even consider randomizing the delay amount by using $RANDOM
as a parameter to - so you could say .sleep
sleep $RANDOM
If $RANDOM
the range of values in is too large for you, applying a modulo operation with an optional additional value to ensure the minimum delay should allow you to sleep for a few seconds. For example, to sleep for a period between 10 and 30 seconds, you could do:
sleep $((10 + RANDOM % 21))
Pause a Bash script until user input occurs
Sometimes, waiting for a specific amount of time just won't cut it - you might want to make sure the user has read your welcome message and/or is ready to start running your script. In this case, you want to wait for them to type something to indicate that they're ready to start.
To do this, we'll use read
the command, which is explained in detail here. read
The command will wait for data to be entered on standard input - the command line where you type commands can be called standard input - and the final Enter keystroke signals to the command that data has been entered. The string can then be put into a variable or simply ignored, which is the use case we'll consider here.
read
The simplest call requires no arguments:
read
It should cause a new line to be printed, where you can type something in. Pressing Enter should return you to the Bash prompt, regardless of what you typed previously.
Combining timed pauses with user input pauses
An excellent use case where you need to combine a timed pause with a user input pause is to ensure that you want the script to continue running even if the user does not respond, and it is not important for them to do so. read
The command allows us -t
to do this by specifying the parameter, which will return to the default state if the input is not pressed .Enter<> 键,该命令将在超时之前等待几秒钟。它将按如下方式使用。
read -t 5
read
Therefore, it will exit when the Enter key is pressed or 5 seconds have passed, whichever comes first .
Another use case is to wait for a specified time specified by the user himself. In this case, you can wait for the user to enter an integer value and then pass it as a parameter sleep
as shown below.
echo -n "Enter time to sleep: "
read $time
sleep $time
This assumes, of course, that the user will sleep
enter a valid argument for .
Let’s review this with some examples!
sleep 3
Will pause the script for exactly 3 seconds.sleep 3d
will pause the script for 3 full days.sleep $N
Specifies$N
the number of seconds to pause the script.sleep $((10 + RANDOM % 21))
Will sleep for a random period of time within 10-30 seconds.read
Waiting for user input.read -t 3
Waits for user input, but times out after 3 seconds if the Enter key is never pressed.read $time && sleep $time
A string parameter will be taken from the user and used assleep
the argument to , allowing the user to specify a sleep time.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to decompress x.tar.xz format files under Linux
Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM
-
A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr
Summary of vim common commands
Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM
-
In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme
Detailed explanation of command return value $? in Linux
Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM
-
? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re
Common judgment formulas for Linux script shell
Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM
-
In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –
Shell script programming practice - specify a directory to delete files
Publish Date:2025/04/08 Views:98 Category:OPERATING SYSTEM
-
Usually, in Linux system we need to frequently delete some temporary files or junk files. If we delete them one by one manually, it will be quite troublesome. I have also been learning shell script programming recently, so I tried to write
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
Use of Linux command crontab - loop execution of set commands
Publish Date:2025/04/08 Views:170 Category:OPERATING SYSTEM
-
Compared with at , which executes a command only once, crontab, which we are going to talk about in this article, executes the set commands in a loop. Similarly, the use of crontab requires the support of the crond service. The service is s
Linux practice - regularly delete files under the directory
Publish Date:2025/04/08 Views:198 Category:OPERATING SYSTEM
-
Since we want to delete the files under the directory regularly, we need to use the Linux crontab command. And the content format of each work routine is also introduced in the format of each crontab work. Similarly, we need to use shell sc
How to use the Linux file remote copy command scp
Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM
-
Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u