Sleep Command in Linux

28/12/2020
Chưa phân loại
Sleep command is used to delay for a fixed amount of time during the execution of any script. When the coder needs to pause the execution of any command for the particular purpose then this command is used with the particular time value. You can set the delay amount by seconds (s), minutes (m), hours (h) and days (d).  This tutorial will help you to learn the use of sleep command by using different bash scripts.

Sleep command syntax:

sleep number[suffix]

You can use any integer or fractional number as time value. Suffix part is optional for this command. If you omit suffix then time value is calculated as seconds by default. You can use s, m, h and d as suffix value. The following examples show the use of sleep command with different suffixes.

Example-1: sleep command without any suffix

In the following script, sleep command is used with numeric value 2 only and no suffix is used. So, if you run the script then the string “Task completed” will print after waiting for 2 seconds.

#!/bin/bash
 
echo "Waiting for 2 seconds…"
sleep 2
echo "Task Completed"

Run the bash file with time command to show the three types of time values to run the script. The output shows the time used by a system, user and real time.

$ time bash sleep1.sh

Output:

Example-2: sleep command with a minute suffix

In the following script, ‘m‘ is used as the suffix with sleep command. Here, the time value is 0.05 minutes. After waiting 0.05 minutes, “Task completed” message will be printed.

#!/bin/bash
 
echo "Waiting for 0.05 minutes…"
sleep 0.05m
echo "Task Completed"

Run the script with time command like the first example.

$ time bash sleep2.sh

Output:

Example-3: sleep command with hour suffix

In the following script, ‘h‘ is used as the suffix with sleep command. Here, the time value is 0.003 hour. After waiting 0.003 hour “Task completed” should be printed on the screen but it requires more times in reality when ‘h’ suffix is used.

#!/bin/bash
 
echo "Waiting for 0.003 hours…"
sleep 0.003h
echo "Task Completed"
 
$ time bash sleep3.sh

Output:

Example-4: sleep command with loop

 You can use sleep command for various purposes. In the following example, sleep command is used with while loop. Initially, the value of the variable n is set to 1 and the value of n will be incremented by 1 for 4 times in every 2 seconds interval. So, when will you run the script, each output will appear after waiting  2 seconds.

#!/bin/bash
n=1
while [ $n -lt 5 ]
do
echo "The value of n is now $n"
sleep 2s
echo " "
((n=$n+1))
done

Output:

Example-5: sleep command in the terminal with other commands

Suppose, you want to run multiple commands and set the fixed time interval between the outputs of two commands, then you can use sleep command to do that task. In this example, the command ls and pwd are with sleep command. After executing the command, ls command will show the directory list of the current directory and show the current working directory path after waiting for 2 seconds.

$ ls && sleep 2 && pwd

Output:

Example-6: Using sleep command from the command prompt

sleep command is used between two echo commands in the following example. Three time values will be displayed after executing the command.

$ time (echo "Start"; sleep 5; echo "End")

Output:

sleep command is a useful command when you need to write a bash script with multiple commands or tasks, the output of any command may require a large amount of time and other command need to wait for completing the task of the previous command. For example, you want to download sequential files and next download can’t be started before completing the previous download. In this case, it is better to sleep command before each download to wait for the fixed amount of time.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Hướng dẫn Fake ip nuôi nick zalo PC bằng Proxy

Mua proxy / Thuê proxy để fake ip nuôi nick Zalo PC ở đâu uy tín Chúng tôi là đơn vị cung cấp proxy uy...
09/11/2022

Five Linux Server Administration Mistakes And How To Avoid Them

In 2017, an employee at GitLab, the version control hosting platform, was asked to replicate a database of production data....
29/12/2020

Find Files on Linux

Introduction Linux is an operating system family which makes use of the Linux kernel. The operating systems under the Linux...
28/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Dịch Vụ Xây Dựng Hệ Thống Peering Với Internet Exchange (IXP)
04/04/2025

Dịch Vụ Triển Khai VPN Site-to-Site & Remote Access
04/04/2025

Dịch Vụ Thiết Lập Hệ Thống Tường Lửa (Firewall)
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ảo Hóa & Cloud
04/04/2025

Dịch Vụ Triển Khai Hệ Thống Ceph
04/04/2025