Home >System Tutorial >LINUX >5 ways related to hibernation waiting in Linux system
In Linux systems, sleep waiting is usually used to save power, or to wait for certain important operations to complete. However, in actual applications, you may encounter various situations where you need to use different sleep waiting methods. This article will introduce you to 5 commonly used Linux hibernation waiting methods to help you better understand and master this technique.
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. The wait command is notified that it has completed until it completes.
Sleep command
sleep command will pause for the specified time. It is commonly used in scripts, but also works on the command line. In the example below, 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
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: $?”
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).
In short, there are many ways to sleep and wait in Linux systems, and each method has its own special application scenarios, advantages and disadvantages. Only with a correct understanding of their basic principles and scope of application can they be used better. I believe that through the introduction of this article, you have a deeper understanding of Linux hibernation waiting.
The above is the detailed content of 5 ways related to hibernation waiting in Linux system. For more information, please follow other related articles on the PHP Chinese website!