JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Pause a Bash Shell Script for X seconds

Author:JIYIK Last Updated:2025/03/20 Views:

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. sleepThe command in Bash can help you achieve this, detailed here. Its syntax is as follows:

sleep n

Where nis the number of seconds you want it to wait. nYou 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.030that should do the trick!

Remember nthat is a mandatory parameter, so sleeprunning without telling how long will not fully work. nRunning 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, sleepit 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 7dthere's a solution for you!

Here are sleepsome 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 nmake _an actual Bash-style variable $Nand 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 $Nthe initialization of , and instead specify a value when you call the script, you can use the value of defined in the shell's environment Nto provide the script with sleepthe 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.shspecify it in the script call by executing

You might even consider randomizing the delay amount by using $RANDOMas a parameter to - so you could say .sleepsleep $RANDOM

If $RANDOMthe 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 readthe command, which is explained in detail here. readThe 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.

readThe 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. readThe command allows us -tto do this by specifying the parameter, which will return to the default state if the input is not pressed .Enter<> 键,该命令将在超时之前等待几秒钟。它将按如下方式使用。

read -t 5

readTherefore, 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 sleepas shown below.

echo -n "Enter time to sleep: "
read $time
sleep $time

This assumes, of course, that the user will sleepenter a valid argument for .

Let’s review this with some examples!

  • sleep 3Will pause the script for exactly 3 seconds.
  • sleep 3dwill pause the script for 3 full days.
  • sleep $NSpecifies $Nthe number of seconds to pause the script.
  • sleep $((10 + RANDOM % 21))Will sleep for a random period of time within 10-30 seconds.
  • readWaiting for user input.
  • read -t 3Waits for user input, but times out after 3 seconds if the Enter key is never pressed.
  • read $time && sleep $timeA string parameter will be taken from the user and used as sleepthe argument to , allowing the user to specify a sleep time.

Previous: None

Next:Making Bash Aliases

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.

Article URL:

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 –

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

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial