Pastebin launched a little side project called HostCabi.net, check it out ;-)Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 20th, 2012  |  syntax: Python  |  size: 0.79 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from signal import SIGTERM
  2. from sys import exit
  3. from time import sleep
  4. from os import *
  5.  
  6. def slow():
  7.     fork()
  8.     sleep(10)
  9.  
  10. def fast():
  11.     sleep(1)
  12.  
  13. child_pids = []
  14. #for child_func in [fast, slow, slow, fast]:
  15. for child_func in [fast, fast]:
  16.     pid = fork()
  17.     if pid == 0:
  18.         setsid()
  19.         child_func()
  20.         exit(0)
  21.     else:
  22.         print "spawned pgrp %d" % pid
  23.         child_pids.append(pid)
  24.  
  25. (pid, status) = waitpid(-1, 0)
  26. print "Reaped pid: %d, status: %d" % (pid, status)
  27. for child_pid in child_pids:
  28.     try:
  29.         print popen('ps -e -o pid,ppid,pgid,state,command | grep -i python').read()
  30.         print "killing pg %d" % child_pid
  31.         killpg(child_pid, SIGTERM)
  32.     except OSError, e:
  33.         print "Error killing %s: %s" %(child_pid, e)