等待一个进程完成 [英] Wait for a process to finish

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

问题描述

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:

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

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

Darwin (requires that $pid has open files):

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

超时(秒)

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天全站免登陆