1

I have a python application which is installed as a console_script on Windows.

I'd like the application to run as a background process for some time and then I would like to terminate it.

I start the process with:

START /B <application_name>

And try to terminate it with:

taskkill /PID <PID>

However when I try and terminate the process, without the force option, I get the error:

ERROR: The process with PID 17836 could not be terminated.
Reason: This process can only be terminated forcefully (with /F option).

Does anyone know how I should extend my Python application so that it can be terminated without the force option?

Not sure if it is important, but I noticed that starting the application actually creates two processes, a python.exe process which seems to be the parent process and the application process.

4
  • Do you have any calls to subprocess module in this python application? Commented Oct 24, 2019 at 9:06
  • Hi @JimErginbash, my application does to make any calls to subprocess. Not sure if this is relevant but it is using aysnc via the aiohttp module. Commented Oct 24, 2019 at 9:11
  • 1
    A non-forceful termination just means that taskkill.exe sends WM_CLOSE to the top-level and message-only windows owned by a process. (Support for closing message-only windows might only be in Windows 10.) Only one console application is considered the effective owner of the console window, which is initially the process that allocates the console. (Omit the /B option of start to have your program allocate a new console.) That's not a reliable state. Instead, you can host a message-only or top-level invisible window that handles WM_CLOSE by initiating a graceful exit. Commented Oct 24, 2019 at 10:40
  • Note that if you target the effective owner of a console window, taskkill.exe won't ask whether you really want to close the console. It just blindly closes it. Consequently, all processes that are attached to the console are sent a control-close event and have 5 seconds to exit before they're forcefully terminated. There is no way for a process that's attached to a console to continue running once the window is closed. At best it can respawn a new process in response to the close event. Commented Oct 24, 2019 at 10:42

1 Answer 1

1

I'm not sure if this does a 'clean' termination of the process, but I found using the /T option for taskkill ensured that all parent and child processes are terminated as well which at least stopped the process.

Ultimately, the command becomes:

taskkill /T /F /PID <pid_number>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.