Advertisement
Guest User

Untitled

a guest
Apr 15th, 2014
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. #def kill_proc_tree(pid, including_parent=True):
  2. # parent = psutil.Process(pid)
  3. # for child in parent.get_children(recursive=True):
  4. # child.kill()
  5. # if including_parent:
  6. # parent.kill()
  7.  
  8. import subprocess
  9. import win32api
  10. import win32com.client
  11. import time
  12. import os
  13. import signal
  14. import psutil
  15.  
  16. # User files
  17. file = "C:DirectorySubDirectoryfile.mcdx"
  18.  
  19.  
  20. # Command line function
  21. # /B /D - No command line window or explorer window
  22. proc = subprocess.Popen('start /B /D
  23. "C:Program FilesPTCMathcadMathcad Prime 2.0" "MathcadPrime.exe"
  24. "C:DirectorySubDirectoryfile.mcdx"', shell=True)
  25.  
  26. # Set and test for active window
  27. focus = False
  28. while (focus == False):
  29. focus = shell.AppActivate("MathCad Prime 2.0 - " + file)
  30. time.sleep(1)
  31. if (focus == True):
  32. break
  33.  
  34. # Send MathCad Prime commands
  35. shell.SendKeys('%', 0)
  36. time.sleep(0.5)
  37. shell.SendKeys('c', 0)
  38. time.sleep(0.1)
  39. shell.SendKeys('c', 0)
  40. time.sleep(2.0)
  41. shell.SendKeys('%', 0)
  42. time.sleep(0.5)
  43. shell.SendKeys('f', 0)
  44. time.sleep(0.1)
  45. shell.SendKeys('s', 0)
  46. time.sleep(4.0)
  47.  
  48.  
  49.  
  50. #Other methods to tried to kill children:
  51.  
  52. #Method 1:
  53. #subprocess.call("TASKKILL /F /T /PID {pid}".format(pid=proc.pid))
  54.  
  55. #Method 2:
  56. #subprocess.Popen("TASKKILL /F /T /PID {pid}".format(pid=proc.pid))
  57.  
  58. #Method 3:
  59. #if (int(proc.pid) > 0):
  60. # os.kill(proc.pid, signal.SIGTERM)
  61.  
  62. #Method 4 (used with kill_proc_tree):
  63. #proc1 = os.getpid()
  64. #kill_proc_tree(proc1)
  65.  
  66. os.chdir('C:Code')
  67. subprocess.call('TASKLIST /fi "IMAGENAME eq MathcadPrime.exe" /nh /fo csv > task.txt)
  68.  
  69. doc = open('task.txt', 'rt')
  70. parse = doc.read()
  71. listing = parse.split(",")
  72. PID = listing[1]
  73. PID = int(PID.replace('"','').replace("'",""))
  74. os.kill(PID, signal.SIGTERM)
  75.  
  76. import subprocess
  77. import os,signal
  78. proc = subprocess.Popen('dir /S', shell=True)
  79. if (int(proc.pid) > 0):
  80. print "killing ",proc.pid
  81. print os.kill(proc.pid, signal.SIGTERM)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement