bash shell sleep_关于bash:等待和睡眠之间的区别

wait和sleep有什么区别?

wait等待进程结束;sleep休眠一定时间。

@域已创建:否,wait 60等待作业60完成

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.

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

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

workhard &

[1] 27408

workharder &

[2] 27409

wait %1 %2

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

如果没有其他背景过程,那么它是wait %1 %2或wait 27408 27409或只是wait。在这种情况下,您试图等待pid 1(init)和pid 2([migration/0]在我的Linux上),但您将收到错误消息,如:-bash: wait: pid 1 is not a child of this shell,并返回退出代码127。

所以到了2年没人意识到这一点。你完全正确,会编辑答案…

我想说,成功的巨魔

猛击

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

wait%1 or wait $PID

wait ${!}

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

睡眠

添加指定时间段的延迟。

sleep NUMBER[SUFFIX]

sleep 5 (sleep five seconds)

试试这个:

sleep 10 &

wait %1

这个答案正在meta上讨论。

只有当您已经知道这些命令的作用时,这个答案才有意义。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值