1

I have a code to kill my application through task manager and it works successfully. However, now I want to first check if the program exists before it kills it, any suggestions?

def close_SAP()"
   call('taskkill /im saplogon.exe /t /f')

if __name__ == '__main__':
   close_SAP()
Kevin
  • 1,974
  • 1
  • 19
  • 51
  • 1
    Possible duplicate of [Python check if a process is running or not](https://stackoverflow.com/questions/7787120/python-check-if-a-process-is-running-or-not) – byxor Feb 19 '18 at 12:16

2 Answers2

2

Here It will check whether the program exists or not and if yes it kill it:

import os
r = os.popen('tasklist /v').read().strip().split('\n')
name  = "saplogon.exe"
for i in range(len(r)):
    if name in r[i]:
        os.system("taskkill /im %s" %(name))
Vikas Periyadath
  • 3,088
  • 1
  • 21
  • 33
0

im assuming youre working on windows since the sap.exe. so this could be helpful for you.

import wmi
c = wmi.WMI ()

for process in c.Win32_Process ():
  print process.ProcessId, process.Name

i cant test it myself since im on a linux machine atm but i got it from here http://timgolden.me.uk/python/wmi/cookbook.html

Molbac
  • 81
  • 9