等待"任何处理"完成 [英] WAIT for "any process" to finish

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

问题描述

时有没有内置的功能在bash等待任何进程结束?
命令只允许一个等待子进程结束。
我想知道是否有任何的方式来等待任何进程在任何脚本继续之前完成。

一个机械的方式来做到这一点如下,但我想知道是否有任何内置的功能在bash。

 而PS -p`$猫&PID_FILE` GT;的/ dev / null的;做睡1; DONE


解决方案

有没有内置。使用杀-0 在一个可行的解决方案的循环:

  anywait(){    在$ @PID;做
        而杀-0$ PID做
            睡0.5
        DONE
    DONE
}

或作为方便一次性使用一个简单的oneliner:

 而杀-0 PIDS 2  - ;的/ dev / null的;做睡1;完成的;

由于注意到一些评论,如果你想等待你没有权限的进程发送信号,你必须找到一些其他的方式,如果该进程正在运行,以取代检测杀-0 $ PID 电话。在Linux上,测试-d的/ proc / $ PID的作品,在其他系统上,你可能必须使用指派, (如有)或类似 PS | grep的^ $ PID

Is there any built in feature in bash to wait for any process to finish? 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.

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

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

解决方案

There's no builtin. Use kill -0 in a loop for a workable solution:

anywait(){

    for pid in "$@"; do
        while kill -0 "$pid"; do
            sleep 0.5
        done
    done
}

Or as a simpler oneliner for easy one time usage:

while kill -0 PIDS 2> /dev/null; do sleep 1; done;

As noted by several commentators, if you want to wait for processes that you do not have the privilege to send signals to, you have find some other way to detect if the process is running to replace the kill -0 $pid call. On Linux, test -d "/proc/$pid" works, on other systems you might have to use pgrep (if available) or something like ps | grep ^$pid.

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

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