Created
November 13, 2011 07:32
-
-
Save jbking/1361759 to your computer and use it in GitHub Desktop.
kill children process recursively.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import subprocess | |
def kill_children_process(pid, force=False): | |
pid = int(pid) | |
for line in subprocess.check_output( | |
'/bin/ps -eo "ppid pid"', | |
shell=True).split('\n')[1:]: | |
line = line.strip() | |
if line: | |
ppid, child_pid = line.split() | |
ppid = int(ppid) | |
child_pid = int(child_pid) | |
if ppid == pid: | |
kill_children_process(child_pid, force=force) | |
if force: | |
os.system('/usr/bin/sudo /bin/kill %d' % child_pid) | |
else: | |
os.system('/bin/kill %d' % child_pid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment