Home > System Tutorial > LINUX > Guide to waiting for hibernation in Linux: Comprehensive analysis of 5 waiting methods

Guide to waiting for hibernation in Linux: Comprehensive analysis of 5 waiting methods

WBOY
Release: 2024-02-12 18:48:03
forward
932 people have browsed it

In Linux, waiting and hibernation are very important concepts. They can help us optimize system resources and improve program performance. However, in actual programming, sometimes we need to let the program "sleep" for a period of time or wait for certain operations to complete before continuing. In this article, we will introduce the wait-to-hibernate method in Linux in detail, so that you can have a deeper understanding of its principles and usage.

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
Copy after login

Linux 中的等待休眠指南:5种等待方式全面解析
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 中的等待休眠指南:5种等待方式全面解析
You can observe changes in memory usage.

[root@localhost ~]# while true ; do free -h ; sleep 5 ; done
Copy after login
Linux 中的等待休眠指南:5种等待方式全面解析

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).

In short, waiting and hibernation are very practical functions in Linux systems, which can help us save system resources and improve program performance. In this article, we introduce 5 common ways to wait for hibernation in Linux, and conduct a detailed analysis of their usage, advantages and disadvantages. I hope that through the introduction of this article, you can better master these wait-and-sleep technologies and use them flexibly in actual programming.

The above is the detailed content of Guide to waiting for hibernation in Linux: Comprehensive analysis of 5 waiting methods. 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