os.killpg寻找

时间:2015-11-04 18:09:35

标签: python subprocess

我正在编写代码以杀死进程和所有子进程。

根据this post,使用os.killpg(pro.pid, signal.SIGTERM)

可以在同一个进程组中杀死所有子进程

在测试过程中,我手动启动了这个过程,产生了5个子过程。

    UID      PID  PPID  C STIME TTY          TIME CMD
    ddd    25066 19475  0 Nov03 ?        00:00:00 /bin/sh -c gtdownload -c ~/.cghub.key --max-children 4 -vv -d https://cghub.ucsc.edu/cghub/data/analysis/download/ab0e89b4-5310-11e4-88da-adc9fc308db6 2
    ddd    25067 25066  0 Nov03 ?        00:00:07 /rsrch1/rists/djiao/apps/cghub/libexec/gtdownload -c /rsrch1/rists/djiao/.cghub.key --max-children 4 -vv -d https://cghub.ucsc.edu/cghub/data/analysis/d
    ddd    25073 25067  0 Nov03 ?        00:00:18 /rsrch1/rists/djiao/apps/cghub/libexec/gtdownload -c /rsrch1/rists/djiao/.cghub.key --max-children 4 -vv -d https://cghub.ucsc.edu/cghub/data/analysis/d
    ddd    25077 25067  0 Nov03 ?        00:00:18 /rsrch1/rists/djiao/apps/cghub/libexec/gtdownload -c /rsrch1/rists/djiao/.cghub.key --max-children 4 -vv -d https://cghub.ucsc.edu/cghub/data/analysis/d
    ddd    25081 25067  0 Nov03 ?        00:00:18 /rsrch1/rists/djiao/apps/cghub/libexec/gtdownload -c /rsrch1/rists/djiao/.cghub.key --max-children 4 -vv -d https://cghub.ucsc.edu/cghub/data/analysis/d
    ddd    25085 25067  0 Nov03 ?        00:00:18 /rsrch1/rists/djiao/apps/cghub/libexec/gtdownload -c /rsrch1/rists/djiao/.cghub.key --max-children 4 -vv -d https://cghub.ucsc.edu/cghub/data/analysis/d

然而,当我运行os.killpg(25066,signal.SIGTERM)时,我收到错误“OSError:[Errno 3]没有这样的过程”。为什么不能找到具有该ID的进程?

1 个答案:

答案 0 :(得分:0)

在调用os.setpgrp()之前,您需要使用os.killpg()设置流程组。 如果您未设置任何进程组,则无法使用os.killpg()

将其终止

您可以使用以下方式创建流程组:

  1. os.setpgrp() # If no argument is passed then it is equivalent to os.setpgrp(0,0). This will create a process group with id same as the calling process id.
  2. os.setpgrp(0, 999) #It will create a process group with id 999 and the current process will be part of that group. You can use any process id instead of 0 to make it part of that process group.
  3. os.setpgrp()实际上调用了linux系统调用setpgrp()。有关详细信息,请参见以下linux手册页 https://linux.die.net/man/2/setpgrp

    http://man7.org/linux/man-pages/man2/setpgid.2.html