Perl,等待非子进程退出 [英] Perl, waiting for non-child process to exit

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

问题描述

我有一个脚本,该脚本用于在自定义服务器环境中重新部署几个程序(即:不是具有代码热交换功能的已建立标准容器).为此,它会关闭服务器进程,但这些进程需要一些时间才能完全关闭其所有连接.这些不是perlscript的子进程.它们通常一次运行数百天,因此,我宁愿不必将服务器进程包装在perlscript中,这样我就可以分叉它们以在数月或数年后优雅地关闭它们.

I have a script which is used to redeploy a couple programs in a custom server environment, (ie: not an established standard container which has code hotswapping). To do this, it takes down the server processes, but these take some time to fully close all their connections. These aren't child processes of the perlscript. They run for hundreds of days at a time normally, so I'd rather not have to wrap the server processes in perlscripts just so I can fork them to shut them down elegantly months or years later.

因此,当前正在等待他们在重新部署期间死亡,我正在解析ps -ef的输出,获取pid字段,杀死该pid,等待60秒(这对于这些进程而言似乎是一个合理的时间),然后重新检查ps -ef以确保它们已死,等等.继续复制,chmod等.

So currently to wait on them to die during redeployment, I'm parsing the output of ps -ef, grabbing the pid field, killing that pid, waiting 60 seconds, (which seems a reasonable time with these processes), rechecking the ps -ef to make sure they're dead, etc. Go on with copies, chmods, etc.

这个解决方案让我感到la脚/笨拙.我已经在Google上搜索过,并且在这个特定主题上还没有看到任何内容.关于等待分叉的孩子的材料很多,而waitpid只有这样操作才是完美的.

This solution feels lame/clunky to me. I've google'd all over and have not seen anything on this particular topic; there's a pile of material about waiting on forked children, and waitpid would be perfect if only it operated in this way.

通过阅读如何等待非孩子退出进程(特定于c)我猜除了可以读取/proc/pid之外,实际上我还有很多其他事情可以做,但是我认为也许会有一个针对Perl的解决方案某处.有什么想法吗?

From reading How to wait for exit of non-children processes (which is c specific)I'm guessing there's really not much else I can do, apart from reading /proc/pid instead, but I thought maybe there'd be a perl-specific solution out there somewhere. Any ideas?

推荐答案

您可以使用kill 0, $pid(成功返回1,失败返回0)而不是重新检查ps -ef,但这可能会导致pid已被重复使用.

You can use kill 0, $pid (returns 1 on success and 0 on failure) instead of rechecking ps -ef, but that has the possible gotcha that the pid may have been reused.

如果您已经有ps解析代码,可能不值得切换,但是有 Proc :: ProcessTable .

If you already have ps-parsing code, it's probably not worth it to switch, but there's Proc::ProcessTable.

除此之外,没有其他想法.

Other than that, no ideas.

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

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