def kill_process_tree(logger, pid):
 try:
  root_process = psutil.Process(pid)
 except psutil.NoSuchProcess:
  logger.warn(u'PID: {} does not exist'.format(pid))
  return
 descendant_processes = [x for x in root_process.children(recursive=True) if x.is_running()]
 if (len(descendant_processes) != 0):
  logger.warn(u'Terminating descendant processes of {} PID: {}'.format(root_process.cmdline(), root_process.pid))
  temp_processes = descendant_processes[:]
  for descendant in temp_processes:
   logger.warn(u'Terminating descendant process {} PID: {}'.format(descendant.cmdline(), descendant.pid))
   try:
    kill_using_shell(descendant.pid, signal.SIGTERM)
   except psutil.NoSuchProcess:
    descendant_processes.remove(descendant)
  logger.warn(u'Waiting up to {}s for processes to exit...'.format(TIME_TO_WAIT_AFTER_SIGTERM))
  try:
   psutil.wait_procs(descendant_processes, TIME_TO_WAIT_AFTER_SIGTERM)
   logger.warn(u'Done waiting')
  except psutil.TimeoutExpired:
   logger.warn(u'Ran out of time while waiting for processes to exit')
  descendant_processes = [x for x in root_process.children(recursive=True) if x.is_running()]
  if (len(descendant_processes) > 0):
   temp_processes = descendant_processes[:]
   for descendant in temp_processes:
    logger.warn(u'Killing descendant process {} PID: {}'.format(descendant.cmdline(), descendant.pid))
    try:
     kill_using_shell(descendant.pid, signal.SIGTERM)
     descendant.wait()
    except psutil.NoSuchProcess:
     descendant_processes.remove(descendant)
   logger.warn(u'Killed all descendant processes of {} PID: {}'.format(root_process.cmdline(), root_process.pid))
 else:
  logger.debug(u'There are no descendant processes to kill')

def kill_process_tree ( logger , pid ) : try : root_process = psutil . Process ( pid ) except psutil . NoSuchProcess : logger . warn ( u'PID: {} does not exist' . format ( pid ) ) return descendant_processes = [ x for x in root_process . children ( recursive = True ) if x . is_running ( ) ] if ( len ( descendant_processes ) != 0 ) : logger . warn ( u'Terminating descendant processes of {} PID: {}' . format ( root_process . cmdline ( ) , root_process . pid ) ) temp_processes = descendant_processes [ : ] for descendant in temp_processes : logger . warn ( u'Terminating descendant process {} PID: {}' . format ( descendant . cmdline ( ) , descendant . pid ) ) try : kill_using_shell ( descendant . pid , signal . SIGTERM ) except psutil . NoSuchProcess : descendant_processes . remove ( descendant ) logger . warn ( u'Waiting up to {}s for processes to exit...' . format ( TIME_TO_WAIT_AFTER_SIGTERM ) ) try : psutil . wait_procs ( descendant_processes , TIME_TO_WAIT_AFTER_SIGTERM ) logger . warn ( u'Done waiting' ) except psutil . TimeoutExpired : logger . warn ( u'Ran out of time while waiting for processes to exit' ) descendant_processes = [ x for x in root_process . children ( recursive = True ) if x . is_running ( ) ] if ( len ( descendant_processes ) > 0 ) : temp_processes = descendant_processes [ : ] for descendant in temp_processes : logger . warn ( u'Killing descendant process {} PID: {}' . format ( descendant . cmdline ( ) , descendant . pid ) ) try : kill_using_shell ( descendant . pid , signal . SIGTERM ) descendant . wait ( ) except psutil . NoSuchProcess : descendant_processes . remove ( descendant ) logger . warn ( u'Killed all descendant processes of {} PID: {}' . format ( root_process . cmdline ( ) , root_process . pid ) ) else : logger . debug ( u'There are no descendant processes to kill' )
signal a process and all of its children .