等待过程完成 [英] Wait for a process to finish

查看:84
本文介绍了等待过程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bash中是否有任何内置功能来等待进程完成?

Is there any builtin feature in Bash to wait for a process to finish?

wait命令仅允许一个人等待子进程完成. 我想知道在执行任何脚本之前是否有任何方法可以等待任何过程完成.

The wait command only allows one to wait for child processes to finish. I would like to know if there is any way to wait for any process to finish before proceeding in any script.

执行此操作的机械方法如下,但我想知道Bash中是否有任何内置功能.<​​/p>

A mechanical way to do this is as follows but I would like to know if there is any builtin feature in Bash.

while ps -p `cat $PID_FILE` > /dev/null; do sleep 1; done

推荐答案

要等待任何过程完成

Linux:

To wait for any process to finish

Linux:

tail --pid=$pid -f /dev/null

Darwin(要求$pid具有打开的文件):

Darwin (requires that $pid has open files):

lsof -p $pid +r 1 &>/dev/null

超时(秒)

Linux:

With timeout (seconds)

Linux:

timeout $timeout tail --pid=$pid -f /dev/null

Darwin(要求$pid具有打开的文件):

Darwin (requires that $pid has open files):

lsof -p $pid +r 1m%s -t | grep -qm1 $(date -v+${timeout}S +%s 2>/dev/null || echo INF)

这篇关于等待过程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆