Thursday, 30 May 2019

process - On Windows, how can I gracefully ask a running program to terminate?


On Unix or Linux, it's easy to gracefully ask a running application to terminate: you send it the SIGTERM signal. If its process ID is 1234, you can simply run kill 1234 or kill -s TERM 1234 or kill -15 1234.


How can I do the same thing in Windows?


If I recall correctly, Task Manager's "End Task" feature (not its "End Process" feature) used to do what I want. But, as of Windows 8.1, it's no longer so gentle; when I use the feature, it causes me to lose unsaved data.


I don't want to write any code in order to do this. If I did, I would ask on Stack Overflow. :)



Answer




Andy E writes that you can use taskkill.exe.


To terminate the process with process ID 1234:


taskkill /pid 1234

To terminate notepad.exe:


taskkill /im notepad.exe

For more help:


taskkill /?

He adds:



The /f switch would force the kill, but not using it just sends the termination signal so the application closes gracefully.



He's right. I tried using taskkill.exe (without /f) to terminate Notepad, and it worked as expected. Notepad asked me whether or not I wanted to save the unsaved text which I'd entered.


I'm using the home version of Windows 8.1. It looks like taskkill.exe came included with Windows at no extra charge. It's in c:\windows\system32.



If you don't want to bother with any of this, simply log out of your Windows user account, then log back in again.



Regarding console applications (e.g. PowerShell), Chris Becke adds:



There is no real way to close console applications gracefully on Windows. They need to support some kind of graceful close mechanism.



No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...