2
votes

I have a batch file which run a python based application which reads a messages continuously. I have python script which closes the application after a timeout period and Execute the Remaining code.

I am using Subprocess.Popen for Batch file run and terminate() call to terminate but the Cmd Window is still in OPen. It is not closing?

And the code is not executing untill the Window Closes. How can i forcly close the cmd?

1
Why are you using a batch file instead of running the Python script directly? - Eryk Sun
It's not a "cmd" window. It's a console window hosted by conhost.exe. It won't close until all attached processes have exited. You're killing cmd.exe, but the child python.exe process is also attached to the console. Do you even need the console window? Try running the child process with creationflag set to either CREATE_NO_WINDOW or DETACHED_PROCESS. - Eryk Sun

1 Answers

1
votes

You can use psutil

And specifically the Process.terminate() function.

lets say your command prompt window name is "myscript"

for proc in psutil.process_iter():
    if proc.name() == "myscript":
        proc.terminate()

Or you can run another command, for instance taskkill

taskkill /F /T /IM myscript