等待和睡眠之间的区别


Answers:


360

wait等待过程完成;sleep睡眠一定的时间。


34
@DomainsFeatured:不,wait 60等待作业60完成
Colin Pitrat '16

115

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不是shell内置命令。它是一个会延迟指定时间的实用程序。

sleep命令可能支持以各种时间单位等待。GNU coreutils 8.4 man 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.

90

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

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

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

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


24
恕我直言,wait %1 %2或者没有其他后台进程,wait 27408 27409或者只是简单地wait说。在这种情况下,您尝试等待PID 1(初始化)和PID 2(在我的Linux上为[migration / 0]),但是您将收到错误消息,例如:-bash: wait: pid 1 is not a child of this shell并返回退出代码127
TrueY 2014年

11
因此,从2年开始,没有人意识到这一点。您绝对正确,将编辑答案...
pbhd 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.