1

I am making a program to do a bit of process management for me on windows 10.

In order to kill unneeded processes I am using the following command:

import subprocess
subprocess.Popen("taskkill /F /im "+unwantedProcess, shell=True, stdout=subprocess.PIPE)

Where unwanted process is the full name of a process I want to kill. I have also tried using:

os.system("taskkill /F /im "+unwantedProcess)

Now this works fine when it is interpreted or compiled using pyinstaller. The problem comes when I use the

--noconsole

command in pyinstaller. It works in all other instances except when there in no console (since I do not want a console opened in the background). Any idea how to fix this?

Edit: After editing the code I inserted a very basic error catch statement as follows:

try:
    subprocess.Popen("taskkill /F /im skype.exe", shell=True, stdout=subprocess.PIPE)
except Exception as e:
    f = open("error.txt", "w")
    f.write(str(e))
    f.close()

Using skype as an example process to kill. It still killed it successfully when the program was interpreted or compiled with a console. However, when it was compiled using the

--noconsole

command once again I got this output in my error file:

[Error 6] The handle is invalid

Skype was definitely open when I ran this program

Adam Griffiths
  • 680
  • 6
  • 26
  • 60

1 Answers1

1

The fact that you are calling python with the --no-console option makes your process not suitable for console I/O (i.e. what you need to call taskkill).

If you really want to hide the console and still have a valid process handle, there are different ways to do it. Look at the answers for this question

Community
  • 1
  • 1
arainone
  • 1,928
  • 17
  • 28