2
votes

In general, we can use Python to execute Windows's cmd command, for example:

 os.system('ipconfig')

but I find that tskill can not be executed by Python, if I use:

os.system('tskill 8684')

to kill a process by its pid, Python will show cmd's error:

'tskill' is not recognized as an internal or external command, operable program or batch file.

but it work well if I use cmd to run the command.

As I know tskill.exe is located in C:\Windows\System32, but this path is not in Python's environment variable. It is maybe the reason, but ipconfig.exe is also in System32, it can be executed.

So why tskill can not be executed by os.system or subprocess.Popen?

2
The message comes from the shell, not from Python. - cdarke
which version of Python do you have? and what about your OS? - Giordano
You could use taskkill instead. - Jokab
@Jokab I have try taskkill, also can not run. - Kingname

2 Answers

3
votes

I have found the root reason:

My Python is 32-bit, while My PC is Windows7 64-bit, so Python's os.system can not run tskill. If I use Python 64-bit instead, everything is OK.

1
votes

Use taskkill, which can do pretty much everything as tskill

But if you want to stick to tskill.exe in your scripts/code. Please run the scripts from elevated command prompts. (Right click on cmd.exe and run it as administrator)

os.system('c:\windows\system32\tskill.exe 8684')