Linux系统:最可靠的方法来终止的进程系列 [英] Linux: the most reliable way to terminate a family of processes

查看:137
本文介绍了Linux系统:最可靠的方法来终止的进程系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是终止一个家庭在Linux进程的最佳方式,如果我们假设:


  1. 在家庭任意进程可以被杀死/终止之前,我们就可以开始清理;作为一个结果,如果子进程不终止,其PPID将1

  2. 进程可以改变进程组

我期待在特定的情况下,这是Bash,但更普遍的技术,就更好了。


解决方案

您可能要执行在不同的登录shell杀害(通过脚本最终),以确保您不会意外停止/查杀非常外壳/脚本试图做全盘查杀其完成作业前:)

第一个关键的策略是不直接终止一个进程,但是:


  • 只是冻结首次(含杀-STOP< PID> ),以prevent它
    产卵其他孩子(需要可靠地确定它的孩子,
    否则你会错过一些如本解释
    Q&安培; A:<一href=\"http://superuser.com/questions/927836/how-to-deal-with-a-memory-leaking-fork-bomb-on-linux/927967#927967\">http://superuser.com/questions/927836/how-to-deal-with-a-memory-leaking-fork-bomb-on-linux/927967#927967)

  • 将其添加到进程列表终止(后下)

  • 找到其子项的列表

  • 迭代对孩子的原委,rince重复

在基于PPID整个祖先树被冻结,你就可以开始基于流程群体定位和冻结祖先 - 你仍然可以决定这些过程组可靠的,只要它改变了自己的进程组仍然活着进程的父母(因为他们的PPID不改变) - 增加这些群体pgids列表进行核爆,并冻结任何新的基于PPID工艺子树,您可以在这些群体像上面找到:


  • 如果他们的父母都还健在,他们应该已经被冷冻
    他们在冰冻的基础PPID-祖先树

  • 如果他们是孤儿当整个PGID将核爆,他们将被杀死

相关过程可以通过在非常类似于根据组ID的一个方式的会话ID被发现(除了杀需要由pid完成作为杀CMD支持一个组ID,但不是一个会话ID)。

另一种方法找到潜在相关的过程将是由他们的TTY,如果他们有一个。但是小心 - 他们可能不是你想要杀掉该进程的后代,但祖先或sibblings。您仍然可以冻结基于PPID子树和群体,而你研究你觉​​得这种方式 - 你可以随时解冻他们后来(与杀-CONT ),如果他们不'吨需要被杀死。

我不知道如何找到一个过程宣称他们自己的领导人会议(从而改变他们的两个SID和PGID)分离后代子树的过程,如果他们的父母死了,他们没有PTY。

在子树的整个列表是冻结的进程可以被杀死(由pid或PGID如果需要)或解冻,继续他们的工作,如果需要的话。

What is the best way to terminate a family of processes in Linux, if we assume that:

  1. arbitrary process in the family can get killed / terminates before we can start cleanup; as a result, if the child processes don't terminate, their PPID will be 1
  2. processes can change process groups

The particular scenario I'm looking at is Bash, but the more general technique, the better.

解决方案

You may want to perform the killing (eventually via a script) in a different login shell to ensure you're not accidentally stopping/killing the very shell/script attempting to do the overall killing before it completes its job :)

The first key strategy is to not directly terminate a process, but to:

Once the entire ancestry tree based on ppid is frozen you can start locating and freezing ancestries based on process groups - you can still determine these process groups reliably as long as the parents of the processes which changed their process group are still alive (since their ppid is not changed) - add these groups to a list of pgids to be nuked and freeze any new ppid-based process subtrees you may find in these groups like above:

  • if their parents are still alive they should be frozen already as they're in the frozen ppid-based ancestry tree
  • if they're orphans they will be killed when the entire pgid will be nuked

Related processes can be discovered by session ID in a manner very similar to the one based on group ID (except killing needs to be done by pid as the kill cmd supports a group ID but not a session ID).

Another way to find potentially related processes would be by their tty, if they have one. But with care - they might not be descendents of the process you want to kill but ancestors or sibblings. You can still freeze the ppid-based subtrees and groups you find this way while you investigate - you can always "thaw" them later (with kill -CONT) if they don't need to be killed.

I don't know how to locate descendant process subtrees decoupled by a processes declaring themselves session leaders (thus changing both their sid and pgid) if their parents died and they have no pty.

Once the entire list of subtrees is frozen processes can be killed (by pid or pgid as needed) or thawed to continue their work if desired.

这篇关于Linux系统:最可靠的方法来终止的进程系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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