等待和睡眠之间的区别

zii*_*web 279 bash shell sleep wait

wait和之间有什么区别sleep

MRA*_*RAB 329

wait等待一个过程完成; sleep睡了一定秒.

  • @DomainsFeatured:不,`等待60'等待作业60完成 (30认同)

kbu*_*ien 106

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.
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)


pbh*_*bhd 86

sleep 只是将shell延迟给定的秒数.

wait使shell等待给定的子进程.例如:

workhard &
[1] 27408
workharder &
[2] 27409
wait %1 %2
Run Code Online (Sandbox Code Playgroud)

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

  • 恕我直言,如果没有其他后台进程,它是`wait%1%2`或`wait 27408 27409`或者只是`wait`.在这种情况下,您正在尝试等待PID 1(init)和PID 2(我的Linux上的[migration/0]),但是您将收到错误消息,例如:`-bash:wait:pid 1不是这个shell`并返回退出代码`127`. (23认同)
  • 所以至于2年没有人意识到这一点.你是绝对正确的,会编辑答案...... (10认同)

jcr*_*ada 24

巴什

wait命令停止脚本执行,直到所有在后台运行的作业终止或者指定作为选项的作业号或进程ID终止

wait%1 or wait $PID
wait ${!}
Run Code Online (Sandbox Code Playgroud)

等待$ {!}表示"等到最后一个后台进程完成"($!是最后一个后台进程的PID)

睡觉

在指定的时间内添加延迟.

sleep NUMBER[SUFFIX]
sleep 5 (sleep five seconds)
Run Code Online (Sandbox Code Playgroud)


lea*_*fei 10

试试这个:

sleep 10 &
wait %1
Run Code Online (Sandbox Code Playgroud)

  • 仅当您已经知道这些命令的功能时,此答案才有意义。 (6认同)