等待后台进程退出脚本前完成 [英] Waiting for background processes to finish before exiting script

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

问题描述

我如何确保我所有的后台进程已经完成执行之前我离开我的脚本(TCL / bash)的。

How do I make sure that all my background processes have finished execution before I exit my script (TCL/Bash).

我想我写的所有后台进程的PID到pidfile进程文件中。然后在年底pgrep命令pidfile进程文件来查看是否有任何进程之前,我退出仍在运行。

I was thinking of writing all my background process pids to a pidfile. And then at the end pgrep the pidfile to see if any processes are still running before I exit.

有一些简单的方法来做到这一点?而且是有没有做到这一点TCL特定的方式?

Is there some simpler way to do this? And is there a TCL specific way to do this?

推荐答案

如果您想等待作业完成,使用。这将使壳等待,直到所有的后台作业完成。但是,如果你的任何作业的守护进程本身,它们的外壳不再是孩子,等待将没有任何效果(至于外壳而言,孩子已经完成了。事实上,当一个进程daemonizes本身,它这样做通过终止和产卵继承其作用的新工艺)。

If you want to wait for jobs to finish, use wait. This will make the shell wait until all background jobs complete. However, if any of your jobs daemonize themselves, they are no longer children of the shell and wait will have no effect (as far as the shell is concerned, the child is already done. Indeed, when a process daemonizes itself, it does so by terminating and spawning a new process that inherits its role).

#!/bin/sh
{ sleep 5; echo waking up after 5 seconds; } &
{ sleep 1; echo waking up after 1 second; } &
wait
echo all jobs are done!

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

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