在 python 中杀死 sudo 启动的子进程 [英] Killing sudo-started subprocess in python

查看:40
本文介绍了在 python 中杀死 sudo 启动的子进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与一个无需提供密码即可进行根级别调用的用户一起运行.我的用户目前正在做这样的事情

I am running with a user that can make root-level calls without having to supply a password. My user currently does something like this

pr = subprocess.Popen("sudo sleep 100".split())
sleep(5)
pr.kill()

但这会导致这个错误,因为用户不是 root,所以它不能杀死一个 root 进程

but that leads to this error because the user isn't root so it can't kill a root process

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/subprocess.py", line 1572, in kill
    self.send_signal(signal.SIGKILL)
  File "/usr/lib/python2.7/subprocess.py", line 1562, in send_signal
    os.kill(self.pid, sig)
OSError: [Errno 1] Operation not permitted

所以我尝试做这样的事情

so I try to do something like this

pr = subprocess.Popen("sudo sleep 100".split())
sleep(5)
kill_pr = subprocess.Popen("sudo kill {}".format(pr.pid))

但这不会终止相关进程.例如,如果

but that doesn't kill the process in question. For example, if

>> subprocess.Popen("sudo sleep 100".split()).pid
5000

但是

$ pgrep sleep
5001

所以看起来从 subprocess.Popen("..").pid 返回的 pid 比运行命令的进程的实际 pid 高一我想杀人

so it seems that the pid returned from subprocess.Popen("..").pid is one higher than the actual pid of the process running the command that I want to kill

我假设 Popen 调用返回的 pid 是父进程,所以我尝试执行类似

I'm assuming that the pid returned from the Popen call is the parent process, so I try doing something like

sudo kill -- -$PID,其中 $PID 是从 Popen 返回的那个,但这只是给了我

sudo kill -- -$PID, where $PID is the one returned from Popen, but that just gives me

kill: sending signal to -2100 failed: No such process

为什么进程不存在?

本质上,我只需要一种方法来使用 sudo 使用 python 的子进程运行命令,然后能够在需要时将其杀死.我假设我需要使用我试图杀死的进程的 pid 或类似的东西运行某种类型的 sudo kill 命令,但我无法确定具体如何执行此操作.

Essentially, I just need a way to run a command with sudo using python's subprocess, then be able to kill it when I need to. I'm assuming I need to run some type of sudo kill command with the pid of the process I'm trying to kill or something like that but I'm unable to determine exactly how to do this.

推荐答案

我想我想通了,问题是如果我这样做了

I think I figured it out, the issue was that if I did this

import subprocess, os
pr = subprocess.Popen(["sudo", "sleep", "100"])
print("Process spawned with PID: %s" % pr.pid)
pgid = os.getpgid(pr.pid)
subprocess.check_output("sudo kill {}".format(pgid))

它会杀死启动python解释器的进程

it would kill the process that started the python interpreter

>>> Terminated

因此,我将 preexec_fn 设置为 os.setpgrp

so instead, I set the preexec_fn to os.setpgrp

import subprocess, os
pr = subprocess.Popen(["sudo", "sleep", "100"], preexec_fn=os.setpgrp)
print("Process spawned with PID: %s" % pr.pid)
pgid = os.getpgid(pr.pid)
subprocess.check_output("sudo kill {}".format(pgid))

如果我检查,在另一个 shell 中

in another shell, if I check

pgrep sleep

什么都没有出现,所以它实际上被杀死了.

nothing shows up, so it is actually killed.

这篇关于在 python 中杀死 sudo 启动的子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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