Home

Tags

Как убить дерево процессов, python

2010-02-28 python

Метод работает в linux и windows

# coding: utf-8

def killTree(pid):
    import platform
    import subprocess
    import re
    pl = platform.system()
    if pl == 'Windows':
        subprocess.Popen('taskkill /PID %d /T /f' % pid, shell = True)
    elif pl == 'Linux':
        PIPE = subprocess.PIPE
        p = subprocess.Popen('pstree -p %d' % pid, shell=True, stdin=PIPE, stdout=PIPE,
                stderr=subprocess.STDOUT, close_fds=True)
        res = ''.join( p.stdout.readlines() )
        d = re.findall('\((\d+)\)',res)
        if d:
            for i in d:
                subprocess.Popen('kill -9 ' + i, shell = True)

comments powered by Disqus