How to Pause Shell Script

How to Pause Shell Script

Often Linux developers and system administrators need to suspend execution of shell script for a while. You can easily do this using sleep or read commands in Linux. These commands are readily available in almost every Linux distribution. In this article, we will learn how to pause shell script.

How to Pause Shell Script

Here are the steps to pause shell script execution using read and sleep commands. You can choose to suspend script execution for a specific duration, or until a keypress event occurs. We will look at both these methods one by one.

1. Using Read Command

If you want to pause shell script until keypress, you need to use read command. Read command pauses shell script and waits for user input via keypress. There is no specific time limit for suspension but execution will continue only after user input. Add the following line to your code location to pause script execution.

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

Here is an example of 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, shell script execution is paused after second echo statement. The user input is displayed on the screen.

If you do not want to display user input, add -s option.

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

By default, read command keeps waiting indefinitely until user provides input. If you want read command to wait only for a specific duration, you can specify it using -t option. Here is an example to wait for 5 seconds.

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

2. Using Sleep Command

You can easily pause shell script execution. Here is the syntax of sleep command.

sleep number[suffix]

In the above command, you need to mention the number of seconds, minutes, hours, etc. you want the shell to wait after sleep command. Here are some examples to pause 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.

You can add sleep command at any point of your script. After the specific time duration, shell script execution will continue. You can also combine different time units as shown below.

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

Here is an example to suspend shell script execution for 5 seconds and then run the 4th statement.

#!/bin/sh

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

In this article, we have learnt how to hold shell script execution in Linux. You can customize it as per your requirement.

Also read:

How to Send HTML Mail in Python
How to Find & Replace String in VI Editor
How to List All Installed Packages in Ubuntu
How to Find Parent PID in Ubuntu
How to Change Default Shell in Ubuntu

Leave a Reply

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