[英]Python SIGTERM not killing subprocess
I have a class which can start and close a process. 我有一个可以启动和关闭过程的类。 However it seems not to close the process.
但是,似乎没有结束该过程。
My python code, there are other methods but they work fine.: 我的python代码,还有其他方法,但是它们可以正常工作。
class KismetInstance:
"""Creates a kismet_server instance"""
def __init__(self, value=False):
logging.basicConfig(format='%(asctime)-15s::: %(message)s')
self.logger = logging.getLogger('kismet_instance')
self.example = value
def __create_kismet_instance__(self):
"""
Create a kismet_server subprocess.
:return:
"""
shell = ['sudo', '/usr/local/bin/kismet_server']
self.logger.debug('Attempting to run: %s', " ".join(shell))
self.kismet = Popen(shell, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=r'./logs', preexec_fn=os.setsid)
def __destroy_kismet_instance__(self):
"""
Kill the subprocess
:return:
"""
os.killpg(os.getpgid(self.kismet.pid), 15)
It can create the subprocess fine. 它可以很好地创建子流程。 But I get this error when I try to kill (no sudo)
但是当我尝试杀死时出现此错误(无须藤)
OSError: [Errno 1] Operation not permitted
And if I run with sudo, the process is still running afterwards. 如果我使用sudo运行,则此过程仍在运行。
pi@raspberrypi ~/project $ ps -A | grep 'kismet'
2912 ? 00:00:00 kismet_server
I managed to fix this. 我设法解决了这个问题。 Turns out the subprocess was respawning itself creating something weird which prevented python from keeping track of it.
事实证明,子进程正在重新生成自身,从而创建了一些奇怪的东西,从而阻止了python对其进行跟踪。
So I had to do this to fix it, However this is not the most elegant solution, and rather dangerouddangerous . 因此,我必须执行此操作来修复它, 但是,这并不是最优雅的解决方案,而是危险的危险 。
Be careful if you use this , because if you enter a term more broad than mine ( 'kismet'
) then you could kill a lot of processes on your system. 如果使用此命令 , 请务必小心 ,因为如果输入的术语比我的更宽泛(
'kismet'
),则可能会杀死系统上的许多进程。
def __destroy_kismet_instance__(self):
"""
Kill the subprocess
:return:
"""
sig = signal.SIGKILL # What signal to send
os.killpg(os.getpgid(self.kismet.pid), sig) # Kill one of them
p_list = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE) # Get All processes on system
out, err = p_list.communicate()
for line in out.splitlines(): # For each line (or process)
if 'kismet' in line: # if 'kismet' appears in its name
pid = int(line.split(None, 1)[0]) # Get the process ID
self.logger.debug("Found: %d", pid)
os.killpg(os.getpgid(pid), sig) # Kill the process
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.