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 cài đặt và sử dụng Postfix để gửi mail trên Ubuntu 18

Postfix là một phầm mềm nguồn mở được dùng để gửi mail (Mail Transfer Agent-MTA). Được phát...
30/12/2020

Recover Removed Files from Windows NTFS Drive using Live Ubuntu DVD

If you’ve mistakenly deleted some files from your Windows NTFS drive and would like to get them back, then this article...
29/12/2020

Openstack Kolla [Phần 2] Hướng dẫn cài đặt Openstack Train multinode bằng Kolla Ansible

Tiếp tục với series hướng dẫn cài đặt OpenStack bằng Kolla Ansible. Ở bài viết lần trước, Onet...
30/12/2020
Bài Viết

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

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024

Thuê địa chỉ IPv4 IPv6 trọn gói ở đâu chất lượng, giá RẺ nhất?
27/05/2024