Home > Article > System Tutorial > How to use Sleep and Wait commands in Linux system
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 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
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:
[root@localhost ~]# date ; sleep 10s ; date Thu Nov 11 05:11:09 EST 2021 Thu Nov 11 05:11:19 EST 2021
The sleep time can also be less than 1 second, just add a .
.
[root@localhost ~]# date ; sleep .1 ; date Thu Nov 11 05:14:03 EST 2021 Thu Nov 11 05:14:03 EST 2021
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
You can observe changes in memory usage.
[root@localhost ~]# while true ; do free -h ; sleep 5 ; done
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: $?”
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.”
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.”
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!