Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #def kill_proc_tree(pid, including_parent=True):
- # parent = psutil.Process(pid)
- # for child in parent.get_children(recursive=True):
- # child.kill()
- # if including_parent:
- # parent.kill()
- import subprocess
- import win32api
- import win32com.client
- import time
- import os
- import signal
- import psutil
- # User files
- file = "C:DirectorySubDirectoryfile.mcdx"
- # Command line function
- # /B /D - No command line window or explorer window
- proc = subprocess.Popen('start /B /D
- "C:Program FilesPTCMathcadMathcad Prime 2.0" "MathcadPrime.exe"
- "C:DirectorySubDirectoryfile.mcdx"', shell=True)
- # Set and test for active window
- focus = False
- while (focus == False):
- focus = shell.AppActivate("MathCad Prime 2.0 - " + file)
- time.sleep(1)
- if (focus == True):
- break
- # Send MathCad Prime commands
- shell.SendKeys('%', 0)
- time.sleep(0.5)
- shell.SendKeys('c', 0)
- time.sleep(0.1)
- shell.SendKeys('c', 0)
- time.sleep(2.0)
- shell.SendKeys('%', 0)
- time.sleep(0.5)
- shell.SendKeys('f', 0)
- time.sleep(0.1)
- shell.SendKeys('s', 0)
- time.sleep(4.0)
- #Other methods to tried to kill children:
- #Method 1:
- #subprocess.call("TASKKILL /F /T /PID {pid}".format(pid=proc.pid))
- #Method 2:
- #subprocess.Popen("TASKKILL /F /T /PID {pid}".format(pid=proc.pid))
- #Method 3:
- #if (int(proc.pid) > 0):
- # os.kill(proc.pid, signal.SIGTERM)
- #Method 4 (used with kill_proc_tree):
- #proc1 = os.getpid()
- #kill_proc_tree(proc1)
- os.chdir('C:Code')
- subprocess.call('TASKLIST /fi "IMAGENAME eq MathcadPrime.exe" /nh /fo csv > task.txt)
- doc = open('task.txt', 'rt')
- parse = doc.read()
- listing = parse.split(",")
- PID = listing[1]
- PID = int(PID.replace('"','').replace("'",""))
- os.kill(PID, signal.SIGTERM)
- import subprocess
- import os,signal
- proc = subprocess.Popen('dir /S', shell=True)
- if (int(proc.pid) > 0):
- print "killing ",proc.pid
- print os.kill(proc.pid, signal.SIGTERM)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement