【问题标题】:Difference between wait and sleep等待和睡眠的区别
【发布时间】:2012-10-29 02:40:09
【问题描述】:

waitsleep 有什么区别?

【问题讨论】:

    标签: bash shell wait sleep


    【解决方案1】:

    wait 等待进程完成; sleep 休眠一定秒数。

    【讨论】:

    • wait 60 等待作业 60 完成; sleep 60 休眠 60 秒。
    【解决方案2】:

    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.
    

    【讨论】:

      【解决方案3】:

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

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

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

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

      【讨论】:

      • 恕我直言,如果没有其他后台进程,它是 wait %1 %2wait 27408 27409 或只是 wait。在这种情况下,您尝试等待 PID 1 (init) 和 PID 2(我的 Linux 上的 [migration/0]),但您会收到错误消息,例如:-bash: wait: pid 1 is not a child of this shell 并返回退出代码 127
      • 所以 2 年没有人意识到这一点。你是绝对正确的,将编辑答案......
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-17
      • 2016-09-03
      • 2017-07-02
      • 1970-01-01
      • 2012-12-03
      相关资源
      最近更新 更多