Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jbking
Created November 13, 2011 07:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbking/1361759 to your computer and use it in GitHub Desktop.
Save jbking/1361759 to your computer and use it in GitHub Desktop.
kill children process recursively.
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