bash 等待和睡眠的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13296863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:55:54  来源:igfitidea点击:

Difference between wait and sleep

bashshellwaitsleep

提问by ziiweb

What is difference between waitand sleep?

wait和 和有什么区别sleep

回答by MRAB

waitwaits for a process to finish; sleepsleeps for a certain amount of seconds.

wait等待一个进程完成;sleep睡眠一定的秒数。

回答by kbulgrien

wait is a BASH built-in command. From man bash:

wait 是 BASH 内置命令。来自man bash

    wait [n ...]
        Wait  for each specified process and return its termination sta-
        tus.  Each n may be a process ID or a job  specification;  if  a
        job  spec  is  given,  all  processes in that job's pipeline are
        waited for.  If n is not given, all currently active child  pro-
        cesses  are  waited  for,  and  the return status is zero.  If n
        specifies a non-existent process or job, the  return  status  is
        127.   Otherwise,  the  return  status is the exit status of the
        last process or job waited for.

sleep is not a shell built-in command. It is a utility that delays for a specified amount of time.

sleep 不是 shell 内置命令。它是一种延迟指定时间量的实用程序。

The sleepcommand may support waiting in various units of time. GNU coreutils 8.4 man sleepsays:

sleep命令可以支持以各种时间单位等待。GNU coreutils 8.4man sleep说:

    SYNOPSIS
        sleep NUMBER[SUFFIX]...

    DESCRIPTION
        Pause for NUMBER seconds.  SUFFIX may be ‘s' for seconds (the default),
        ‘m' for minutes, ‘h' for hours or ‘d' for days.  Unlike most  implemen-
        tations  that require NUMBER be an integer, here NUMBER may be an arbi-
        trary floating point number.  Given two or more  arguments,  pause  for
        the amount of time specified by the sum of their values.

回答by pbhd

sleepjust delays the shell for the given amount of seconds.

sleep只是将外壳延迟给定的秒数。

waitmakes the shell wait for the given job. e.g.:

wait使外壳等待给定的作业。例如:

workhard &
[1] 27408
workharder &
[2] 27409
wait %1 %2

delays the shell until both of the subprocesses have finished

延迟 shell 直到两个子进程都完成