Home > System Tutorial > LINUX > body text

How to use Sleep and Wait commands in Linux system

王林
Release: 2024-02-13 09:30:14
forward
865 people have browsed it

The sleep and wait commands in Linux allow you to run commands at a selected speed, or capture and display the exit status of a task after waiting for it to complete. sleep simply inserts a timed pause between commands, while the wait command notifies you that it's done until it's done.

Sleep command

sleep command will pause for the specified time. It is typically used in scripts, but can also be used from the command line. In the following example, sleep pauses for 30 seconds between two date commands.

[root@localhost ~]# date ; sleep 30 ; date
Thu Nov 11 05:06:44 EST 2021
Thu Nov 11 05:07:14 EST 2021
Copy after login
Linux 系统中 Sleep 和 Wait 命令的使用方式

The sleep command takes a numeric argument as the number of seconds. But you can also make it sleep for a different time by adding a character in the parameter:

  • 10s = 10 seconds
  • 1m = 1 minute
  • 2h = 2 hours
  • 3d = 3 days
[root@localhost ~]# date ; sleep 10s ; date
Thu Nov 11 05:11:09 EST 2021
Thu Nov 11 05:11:19 EST 2021
Copy after login

The sleep time can also be less than 1 second, just add a ..

in front of the number.
  • .1 = tenth of a second
  • .01 = one hundredth of a second
  • .001 = thousandth of a second
[root@localhost ~]# date ; sleep .1 ; date
Thu Nov 11 05:14:03 EST 2021
Thu Nov 11 05:14:03 EST 2021
Copy after login

sleep command is typically used to run a command periodically, such as every 10 seconds or every minute, when you want to monitor some activity. For example, you might be waiting for a coworker to log into a file server. You might run a command like this:

[root@localhost ~]# while true ; do who ; sleep 100 ; done
Copy after login
Linux 系统中 Sleep 和 Wait 命令的使用方式

You can observe changes in memory usage.

[root@localhost ~]# while true ; do free -h ; sleep 5 ; done
Copy after login
Linux 系统中 Sleep 和 Wait 命令的使用方式

Wait command

The wait command captures the exit status of the background process after waiting for the background process to complete. You can also use it in scripts. Replace the sleep command with the process you want to wait for. This script also displays the exit status.

#!/bin/bash

sleep 5 &
process_id=$!
echo “PID: $process_id”
wait $process_id
echo “Exit status: $?”
Copy after login

The first line starts the sleep command in the background. The second line gets the process ID$! of the most recently executed background process. The script then displays the message, waits for the process to complete and displays the exit status.

If you use wait -n (no additional parameters required), wait will wait for any background tasks to complete. In the example below, the sleep 6 command completes first and the script ends, but the other two sleep processes will continue to run in the background for a few seconds.

#!/bin/bash

sleep 15 &
sleep 9 &
sleep 6 &
wait -n
echo “First job has been completed.”
Copy after login

If changed to the following script, it will tell you when each sleep process is completed. Due to timing, this will happen in the reverse order from when the sleep process began.

#!/bin/bash

sleep 15 &
sleep 9 &
sleep 6 &
wait -n
echo “First job has been completed.”
wait -n
echo “Next job has been completed.”
wait
echo “All jobs have been completed.”
Copy after login

Note: The waiting process must be a "child process" (a process started in the current shell).

Summarize

Both can be used in scripts or the command line. However, the wait command only works on processes running in the background.

The above is the detailed content of How to use Sleep and Wait commands in Linux system. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template