pause shell script

How to Pause Shell Script

Sometimes you may need to pause or suspend shell script execution in Linux. You can do it a couple of ways using sleep or read commands. In this article, we will look at the different ways to pause shell script in Linux. You can use these commands in any Linux distribution since they are commonly available.


How to Pause Shell Script

We will look at how to pause shell script using sleep and read commands.


1. Using Sleep

It is very easy to pause shell script execution using sleep command. Here is its syntax.

sleep number[suffix]

In the above command you need to mention the number of seconds, minutes, hours, etc you want the shell to sleep after sleep command. Here are some examples to suspend shell script

$ sleep .5 # Waits 0.5 second. 
$ sleep 5  # Waits 5 seconds. 
$ sleep 5s # Waits 5 seconds. 
$ sleep 5m # Waits 5 minutes. 
$ sleep 5h # Waits 5 hours. 
$ sleep 5d # Waits 5 days.

Add the above lines anywhere in your shell script, and the execution will be paused at that point for the time duration specified by you. After the time interval, shell script will continue with the next command.

You can also combine time units in sleep command (e.g 1h 30m ) as shown below

$ sleep 1h 30m # Waits 1h 30 minutes.

Here is an example where execution pauses after second echo statement for 5 seconds and runs the third statement only after the time interval.

#!/bin/sh

echo "shell script execution started"
echo "shell script to be paused for 5 secs"
sleep 5
echo "shell script resumed"


2. Using Read

If you want to suspend shell script until keypress, you need to use read command, which waits for user input. Just add the following line in your shell script to pause execution and wait for input.

read -p "Press any key to resume ..."

Here is an example in a shell script

#!/bin/sh

echo "execution started"
echo "execution to be paused"
read -p "Press any key to resume ..."
echo "execution resumed"

In the above code, the shell script execution pauses after second echo statement, and proceeds only after user input.

If you don’t want to display user input, you can add -s option to read statement.

read -s -p "Press any key to resume ..."

You can also combine sleep command with read command to pause execution for a specific amount of time. Here is an example.

read -t 5 -p "I am going to wait for 5 seconds ..."

In the above code, we use -t option to specify the time duration for which read statement should wait. This is very useful because it pauses execution for a specific time as well as displays a message to the user.

In this article, we have learnt different ways to pause shell script execution. You may use any of them as per your requirement.

Also read:

How to Send HTML Email in Python
How to List All Installed Packages in Ubuntu
How to Find & Replace String in VI Editor
How to Find Parent Process ID in Linux
How to Change Default Shell in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *