1
votes

I'm trying to kill an .exe with subprocess.Popen.

Solution as:

  1. taskkill /im (.exe) /t /f
  2. taskkill /pid (.exe) /t /f

aren't working as the response is access denied. Indeed, as i'm running the cmd from subprocess i'm not able to obtain admin privilegies.

I've found a command to kill this process from cmd (without running it as admin) which is:

  1. wmic process where name=".exe" delete

... but when i'm running it with subprocess it gives me "invalid query". The command i'm running is:

4)subprocess.Popen(['wmic', 'process', 'where', 'name="-------.exe"', 'delete'], shell=True, stdout=subprocess.PIPE)

I suppose that i'm writing it wrongly. Some advices?

1
Try removing the quotes around the filename, since you are specifying separate arguments here anyway. Does that work? - CherryDT
Sounds like some weird escaping incompatibility... A shot in the dark and probably not the best solution: ['cmd', '/c', 'wmic process where name="openvpn.exe" delete'] or maybe just pass the whole thing as string: subprocess.popen('wmic process where name="openvpn.exe" delete') - CherryDT
Next thing I would do is use Process Monitor to see what command line is actually passed to wmic (set filter to "operation" "begins with" "process") and compare it to how it looks when you do it from cmd where it works. That may give a clue about what's wrong. - CherryDT
Oh, cool. In that case, it would be nice if you would write an answer to your own question, so that future readers can find the solution too! You can later also self-accept the answer. - CherryDT
You should not mix a list of arguments with shell=True; it sometimes happens to work in weird ways, but is basically always an error. See also stackoverflow.com/questions/3172470/… - tripleee

1 Answers

1
votes

The solution is to put spaces around the comparison operator and send the whole condition (name = xxx or name like xxx) as single argument, since wmic doesn't like it to be split in parts, and internally the quotes get messed up if they are in the "middle" of a word:

subprocess.Popen(['wmic', 'process', 'where', 'name like "----.exe"', 'delete'], shell=True)