Perl,等待非子进程退出
我有一个脚本,用于在自定义服务器环境中重新部署几个程序(即:不是具有代码热交换的既定标准容器)。为此,它会关闭服务器进程,但需要一些时间才能完全关闭所有连接。这些不是 perlscript 的子进程。它们通常一次运行数百天,所以我宁愿不必将服务器进程包装在 perlscripts 中,这样我就可以分叉它们,以便在几个月或几年后优雅地关闭它们。
因此,目前为了等待它们在重新部署期间死亡,我正在解析 ps -ef 的输出,获取 pid 字段,杀死该 pid,等待 60 秒(对于这些来说,这似乎是一个合理的时间)进程),重新检查 ps -ef 以确保它们已死亡,等等。继续进行副本、chmod 等。
这个解决方案对我来说感觉很蹩脚/笨重。我用谷歌搜索了一遍,但没有看到任何关于这个特定主题的内容;有一堆关于等待分叉子进程的材料,如果 waitpid 以这种方式运行,那就完美了。
从阅读如何等待非子进程退出 (这是 c
特定的)我猜除了读取 /proc/pid 之外,我真的无能为力,但我想也许会有一个特定于 perl 的解决方案就在外面的某个地方。有什么想法吗?
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.
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.
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
kill 0, $pid
(成功时返回 1,失败时返回 0)而不是重新检查 ps -ef,但这可能存在 pid 可能已被重用的问题。如果您已经有了 ps 解析代码,可能不值得切换,但是有 Proc::ProcessTable。
除此之外,没有任何想法。
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.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.
在 Unix \ Linux 中,当进程退出父进程时,只有父进程会收到信号 - 这是操作系统功能,而不是特定于语言的。
其他解决方案将与您的解决方案等效 - 检查进程表是否存在进程(尽管具体方法可能有所不同 - 例如使用 ps 或直接查询内核)
In Unix \ Linux only the parent process gets a signal when a process exits parent process - This is an OS feature, and not language specific.
Other solutions will be equivalent to yours - checking the process table for the existence of the process (although the specific method may vary - like using ps or directly querying the kernel)