Terminating a Process from CMD: Softest to Hardest
Terminating a Process from CMD: Softest to Hardest
I was wondering if anyone had experience using command line to terminate processes using taskkill and WIMC.
I was wondering if anyone knew the order of how "hard" of a close/terminate these commands are from the command that is the "softest" (least forceful) close to the command that is the "hardest" (most forceful):
My guess would be:
Least/Softest
1) taskkill /im processname.exe
2) wmic process where name="processname.exe" call terminate
3) wmic process where name='processname.exe' delete
4) taskkill /f /im processname.exe
Most/Hardest
I am trying to create a batch command file and wanted to just know the difference between these, to see which I should use.
I prefer to use a softer close, check to see if the process is still running, and then try a harder close, and then repeat this until the program is successfully closed. Any info on the difference between any of these would be helpful, especially between using terminate and delete via CMD: WMIC would be helpful, as I cannot find documentation anywhere on them.
batch-file cmd process wmic taskkill
|
show 6 more comments
Terminating a Process from CMD: Softest to Hardest
I was wondering if anyone had experience using command line to terminate processes using taskkill and WIMC.
I was wondering if anyone knew the order of how "hard" of a close/terminate these commands are from the command that is the "softest" (least forceful) close to the command that is the "hardest" (most forceful):
My guess would be:
Least/Softest
1) taskkill /im processname.exe
2) wmic process where name="processname.exe" call terminate
3) wmic process where name='processname.exe' delete
4) taskkill /f /im processname.exe
Most/Hardest
I am trying to create a batch command file and wanted to just know the difference between these, to see which I should use.
I prefer to use a softer close, check to see if the process is still running, and then try a harder close, and then repeat this until the program is successfully closed. Any info on the difference between any of these would be helpful, especially between using terminate and delete via CMD: WMIC would be helpful, as I cannot find documentation anywhere on them.
batch-file cmd process wmic taskkill
Taskkill should be pretty obvious. The /F means forcefully terminate.
– Squashman
Nov 14 '18 at 20:30
Taskkill also uses WMI Win32_Process class.
– CatCat
Nov 14 '18 at 20:37
1
For a graphical program aWM_Close
message is sent to the window asking it to close. A program sends one to itself if it wants to close. Programs choose to process this message and can ignore it and do so like Do you want to save this file Yes/No .TerminateProcess
the program does not get informed, no code runs in the program. It is destroyed.
– CatCat
Nov 14 '18 at 20:52
Console programs are similar. Instead ofWM_Close
it gets a control signal. Windows creates a thread that callsExitProcess
(programs can change this). See docs.microsoft.com/en-us/windows/console/…. Note there are only two ways to kill a program, ask it to close or destroy it. WMIC always destroys. In Task Manager (Win 7 here) the Application tab asks programs to close and the Process tab destroys applications.
– CatCat
Nov 14 '18 at 21:51
Also see docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/… and docs.microsoft.com/en-us/windows/desktop/winmsg/wm-close
– CatCat
Nov 14 '18 at 21:51
|
show 6 more comments
Terminating a Process from CMD: Softest to Hardest
I was wondering if anyone had experience using command line to terminate processes using taskkill and WIMC.
I was wondering if anyone knew the order of how "hard" of a close/terminate these commands are from the command that is the "softest" (least forceful) close to the command that is the "hardest" (most forceful):
My guess would be:
Least/Softest
1) taskkill /im processname.exe
2) wmic process where name="processname.exe" call terminate
3) wmic process where name='processname.exe' delete
4) taskkill /f /im processname.exe
Most/Hardest
I am trying to create a batch command file and wanted to just know the difference between these, to see which I should use.
I prefer to use a softer close, check to see if the process is still running, and then try a harder close, and then repeat this until the program is successfully closed. Any info on the difference between any of these would be helpful, especially between using terminate and delete via CMD: WMIC would be helpful, as I cannot find documentation anywhere on them.
batch-file cmd process wmic taskkill
Terminating a Process from CMD: Softest to Hardest
I was wondering if anyone had experience using command line to terminate processes using taskkill and WIMC.
I was wondering if anyone knew the order of how "hard" of a close/terminate these commands are from the command that is the "softest" (least forceful) close to the command that is the "hardest" (most forceful):
My guess would be:
Least/Softest
1) taskkill /im processname.exe
2) wmic process where name="processname.exe" call terminate
3) wmic process where name='processname.exe' delete
4) taskkill /f /im processname.exe
Most/Hardest
I am trying to create a batch command file and wanted to just know the difference between these, to see which I should use.
I prefer to use a softer close, check to see if the process is still running, and then try a harder close, and then repeat this until the program is successfully closed. Any info on the difference between any of these would be helpful, especially between using terminate and delete via CMD: WMIC would be helpful, as I cannot find documentation anywhere on them.
batch-file cmd process wmic taskkill
batch-file cmd process wmic taskkill
asked Nov 14 '18 at 20:20
user149483user149483
61
61
Taskkill should be pretty obvious. The /F means forcefully terminate.
– Squashman
Nov 14 '18 at 20:30
Taskkill also uses WMI Win32_Process class.
– CatCat
Nov 14 '18 at 20:37
1
For a graphical program aWM_Close
message is sent to the window asking it to close. A program sends one to itself if it wants to close. Programs choose to process this message and can ignore it and do so like Do you want to save this file Yes/No .TerminateProcess
the program does not get informed, no code runs in the program. It is destroyed.
– CatCat
Nov 14 '18 at 20:52
Console programs are similar. Instead ofWM_Close
it gets a control signal. Windows creates a thread that callsExitProcess
(programs can change this). See docs.microsoft.com/en-us/windows/console/…. Note there are only two ways to kill a program, ask it to close or destroy it. WMIC always destroys. In Task Manager (Win 7 here) the Application tab asks programs to close and the Process tab destroys applications.
– CatCat
Nov 14 '18 at 21:51
Also see docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/… and docs.microsoft.com/en-us/windows/desktop/winmsg/wm-close
– CatCat
Nov 14 '18 at 21:51
|
show 6 more comments
Taskkill should be pretty obvious. The /F means forcefully terminate.
– Squashman
Nov 14 '18 at 20:30
Taskkill also uses WMI Win32_Process class.
– CatCat
Nov 14 '18 at 20:37
1
For a graphical program aWM_Close
message is sent to the window asking it to close. A program sends one to itself if it wants to close. Programs choose to process this message and can ignore it and do so like Do you want to save this file Yes/No .TerminateProcess
the program does not get informed, no code runs in the program. It is destroyed.
– CatCat
Nov 14 '18 at 20:52
Console programs are similar. Instead ofWM_Close
it gets a control signal. Windows creates a thread that callsExitProcess
(programs can change this). See docs.microsoft.com/en-us/windows/console/…. Note there are only two ways to kill a program, ask it to close or destroy it. WMIC always destroys. In Task Manager (Win 7 here) the Application tab asks programs to close and the Process tab destroys applications.
– CatCat
Nov 14 '18 at 21:51
Also see docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/… and docs.microsoft.com/en-us/windows/desktop/winmsg/wm-close
– CatCat
Nov 14 '18 at 21:51
Taskkill should be pretty obvious. The /F means forcefully terminate.
– Squashman
Nov 14 '18 at 20:30
Taskkill should be pretty obvious. The /F means forcefully terminate.
– Squashman
Nov 14 '18 at 20:30
Taskkill also uses WMI Win32_Process class.
– CatCat
Nov 14 '18 at 20:37
Taskkill also uses WMI Win32_Process class.
– CatCat
Nov 14 '18 at 20:37
1
1
For a graphical program a
WM_Close
message is sent to the window asking it to close. A program sends one to itself if it wants to close. Programs choose to process this message and can ignore it and do so like Do you want to save this file Yes/No . TerminateProcess
the program does not get informed, no code runs in the program. It is destroyed.– CatCat
Nov 14 '18 at 20:52
For a graphical program a
WM_Close
message is sent to the window asking it to close. A program sends one to itself if it wants to close. Programs choose to process this message and can ignore it and do so like Do you want to save this file Yes/No . TerminateProcess
the program does not get informed, no code runs in the program. It is destroyed.– CatCat
Nov 14 '18 at 20:52
Console programs are similar. Instead of
WM_Close
it gets a control signal. Windows creates a thread that calls ExitProcess
(programs can change this). See docs.microsoft.com/en-us/windows/console/…. Note there are only two ways to kill a program, ask it to close or destroy it. WMIC always destroys. In Task Manager (Win 7 here) the Application tab asks programs to close and the Process tab destroys applications.– CatCat
Nov 14 '18 at 21:51
Console programs are similar. Instead of
WM_Close
it gets a control signal. Windows creates a thread that calls ExitProcess
(programs can change this). See docs.microsoft.com/en-us/windows/console/…. Note there are only two ways to kill a program, ask it to close or destroy it. WMIC always destroys. In Task Manager (Win 7 here) the Application tab asks programs to close and the Process tab destroys applications.– CatCat
Nov 14 '18 at 21:51
Also see docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/… and docs.microsoft.com/en-us/windows/desktop/winmsg/wm-close
– CatCat
Nov 14 '18 at 21:51
Also see docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/… and docs.microsoft.com/en-us/windows/desktop/winmsg/wm-close
– CatCat
Nov 14 '18 at 21:51
|
show 6 more comments
1 Answer
1
active
oldest
votes
As CatCat mentioned, there are two main ways to terminate a process : WM_CLOSE
and TerminateProcess()
. I've included two more for completeness sake.
Sending window message
WM_CLOSE
to the main window of the process. This is the same message an application receives when user clicksX
button to close the window. The app may then gracefully shutdown or ask user for confirmation - for example if some work is unsaved.taskkill
without/f
appears to attempt doing that but seems to not always succeed in finding the correct window to close. If the app is supposed to not have a visible window (such as if it only displays an icon in system tray or is a windowless server) it may ignore this message entirely.If
taskkill
does not work for you, it is possible NirCmd: does better job:NirCmd.exe closeprocess iexplore.exe
There is also
WM_ENDSESSION
message - it it sent by the OS when shutting down (afterWM_QUERYENDSESSION
). It works pretty much the same way except it is sent to whole application rather then a specific window. Depending on parameters, apps may be requested to save the work into temporary files because the system needs to restart to apply some updates. Some applications react to this message, some don't.It should be possible to send these messages manually, but I have not seen it done other than to test how app reacts to shutdown without actually shutting down OS.
WM_QUIT
message suggests the application as a whole needs to shut down (more specifically, it is sent to a thread). An application should normally post it to itself after its window is done closing and now it is time to end the process.It is possible to manually post the message to every thread of another process but this is hackish and rare, it may crash processes not expecting to be issued this message from outside. I'm not sure if it's a better option than just terminating the process or not.
TerminateProcess()
tells the OS to forcefully terminate the process. This is what happens when you clickEnd process
button onprocesses
tab in thetask manager
. The process does not get notified it is being closed - it is just stopped where it was and removed from the memory - no questions, no shutdown, etc.
This may cause corruption if some files were being written at that time or data transferred.That is what
taskkill /f
command does. Bothwmic process call terminate
andwmic process delete
appear to also do this although I'm not sure.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308169%2fterminating-a-process-from-cmd-softest-to-hardest%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As CatCat mentioned, there are two main ways to terminate a process : WM_CLOSE
and TerminateProcess()
. I've included two more for completeness sake.
Sending window message
WM_CLOSE
to the main window of the process. This is the same message an application receives when user clicksX
button to close the window. The app may then gracefully shutdown or ask user for confirmation - for example if some work is unsaved.taskkill
without/f
appears to attempt doing that but seems to not always succeed in finding the correct window to close. If the app is supposed to not have a visible window (such as if it only displays an icon in system tray or is a windowless server) it may ignore this message entirely.If
taskkill
does not work for you, it is possible NirCmd: does better job:NirCmd.exe closeprocess iexplore.exe
There is also
WM_ENDSESSION
message - it it sent by the OS when shutting down (afterWM_QUERYENDSESSION
). It works pretty much the same way except it is sent to whole application rather then a specific window. Depending on parameters, apps may be requested to save the work into temporary files because the system needs to restart to apply some updates. Some applications react to this message, some don't.It should be possible to send these messages manually, but I have not seen it done other than to test how app reacts to shutdown without actually shutting down OS.
WM_QUIT
message suggests the application as a whole needs to shut down (more specifically, it is sent to a thread). An application should normally post it to itself after its window is done closing and now it is time to end the process.It is possible to manually post the message to every thread of another process but this is hackish and rare, it may crash processes not expecting to be issued this message from outside. I'm not sure if it's a better option than just terminating the process or not.
TerminateProcess()
tells the OS to forcefully terminate the process. This is what happens when you clickEnd process
button onprocesses
tab in thetask manager
. The process does not get notified it is being closed - it is just stopped where it was and removed from the memory - no questions, no shutdown, etc.
This may cause corruption if some files were being written at that time or data transferred.That is what
taskkill /f
command does. Bothwmic process call terminate
andwmic process delete
appear to also do this although I'm not sure.
add a comment |
As CatCat mentioned, there are two main ways to terminate a process : WM_CLOSE
and TerminateProcess()
. I've included two more for completeness sake.
Sending window message
WM_CLOSE
to the main window of the process. This is the same message an application receives when user clicksX
button to close the window. The app may then gracefully shutdown or ask user for confirmation - for example if some work is unsaved.taskkill
without/f
appears to attempt doing that but seems to not always succeed in finding the correct window to close. If the app is supposed to not have a visible window (such as if it only displays an icon in system tray or is a windowless server) it may ignore this message entirely.If
taskkill
does not work for you, it is possible NirCmd: does better job:NirCmd.exe closeprocess iexplore.exe
There is also
WM_ENDSESSION
message - it it sent by the OS when shutting down (afterWM_QUERYENDSESSION
). It works pretty much the same way except it is sent to whole application rather then a specific window. Depending on parameters, apps may be requested to save the work into temporary files because the system needs to restart to apply some updates. Some applications react to this message, some don't.It should be possible to send these messages manually, but I have not seen it done other than to test how app reacts to shutdown without actually shutting down OS.
WM_QUIT
message suggests the application as a whole needs to shut down (more specifically, it is sent to a thread). An application should normally post it to itself after its window is done closing and now it is time to end the process.It is possible to manually post the message to every thread of another process but this is hackish and rare, it may crash processes not expecting to be issued this message from outside. I'm not sure if it's a better option than just terminating the process or not.
TerminateProcess()
tells the OS to forcefully terminate the process. This is what happens when you clickEnd process
button onprocesses
tab in thetask manager
. The process does not get notified it is being closed - it is just stopped where it was and removed from the memory - no questions, no shutdown, etc.
This may cause corruption if some files were being written at that time or data transferred.That is what
taskkill /f
command does. Bothwmic process call terminate
andwmic process delete
appear to also do this although I'm not sure.
add a comment |
As CatCat mentioned, there are two main ways to terminate a process : WM_CLOSE
and TerminateProcess()
. I've included two more for completeness sake.
Sending window message
WM_CLOSE
to the main window of the process. This is the same message an application receives when user clicksX
button to close the window. The app may then gracefully shutdown or ask user for confirmation - for example if some work is unsaved.taskkill
without/f
appears to attempt doing that but seems to not always succeed in finding the correct window to close. If the app is supposed to not have a visible window (such as if it only displays an icon in system tray or is a windowless server) it may ignore this message entirely.If
taskkill
does not work for you, it is possible NirCmd: does better job:NirCmd.exe closeprocess iexplore.exe
There is also
WM_ENDSESSION
message - it it sent by the OS when shutting down (afterWM_QUERYENDSESSION
). It works pretty much the same way except it is sent to whole application rather then a specific window. Depending on parameters, apps may be requested to save the work into temporary files because the system needs to restart to apply some updates. Some applications react to this message, some don't.It should be possible to send these messages manually, but I have not seen it done other than to test how app reacts to shutdown without actually shutting down OS.
WM_QUIT
message suggests the application as a whole needs to shut down (more specifically, it is sent to a thread). An application should normally post it to itself after its window is done closing and now it is time to end the process.It is possible to manually post the message to every thread of another process but this is hackish and rare, it may crash processes not expecting to be issued this message from outside. I'm not sure if it's a better option than just terminating the process or not.
TerminateProcess()
tells the OS to forcefully terminate the process. This is what happens when you clickEnd process
button onprocesses
tab in thetask manager
. The process does not get notified it is being closed - it is just stopped where it was and removed from the memory - no questions, no shutdown, etc.
This may cause corruption if some files were being written at that time or data transferred.That is what
taskkill /f
command does. Bothwmic process call terminate
andwmic process delete
appear to also do this although I'm not sure.
As CatCat mentioned, there are two main ways to terminate a process : WM_CLOSE
and TerminateProcess()
. I've included two more for completeness sake.
Sending window message
WM_CLOSE
to the main window of the process. This is the same message an application receives when user clicksX
button to close the window. The app may then gracefully shutdown or ask user for confirmation - for example if some work is unsaved.taskkill
without/f
appears to attempt doing that but seems to not always succeed in finding the correct window to close. If the app is supposed to not have a visible window (such as if it only displays an icon in system tray or is a windowless server) it may ignore this message entirely.If
taskkill
does not work for you, it is possible NirCmd: does better job:NirCmd.exe closeprocess iexplore.exe
There is also
WM_ENDSESSION
message - it it sent by the OS when shutting down (afterWM_QUERYENDSESSION
). It works pretty much the same way except it is sent to whole application rather then a specific window. Depending on parameters, apps may be requested to save the work into temporary files because the system needs to restart to apply some updates. Some applications react to this message, some don't.It should be possible to send these messages manually, but I have not seen it done other than to test how app reacts to shutdown without actually shutting down OS.
WM_QUIT
message suggests the application as a whole needs to shut down (more specifically, it is sent to a thread). An application should normally post it to itself after its window is done closing and now it is time to end the process.It is possible to manually post the message to every thread of another process but this is hackish and rare, it may crash processes not expecting to be issued this message from outside. I'm not sure if it's a better option than just terminating the process or not.
TerminateProcess()
tells the OS to forcefully terminate the process. This is what happens when you clickEnd process
button onprocesses
tab in thetask manager
. The process does not get notified it is being closed - it is just stopped where it was and removed from the memory - no questions, no shutdown, etc.
This may cause corruption if some files were being written at that time or data transferred.That is what
taskkill /f
command does. Bothwmic process call terminate
andwmic process delete
appear to also do this although I'm not sure.
answered Nov 14 '18 at 22:24
Jack WhiteJack White
33416
33416
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308169%2fterminating-a-process-from-cmd-softest-to-hardest%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Taskkill should be pretty obvious. The /F means forcefully terminate.
– Squashman
Nov 14 '18 at 20:30
Taskkill also uses WMI Win32_Process class.
– CatCat
Nov 14 '18 at 20:37
1
For a graphical program a
WM_Close
message is sent to the window asking it to close. A program sends one to itself if it wants to close. Programs choose to process this message and can ignore it and do so like Do you want to save this file Yes/No .TerminateProcess
the program does not get informed, no code runs in the program. It is destroyed.– CatCat
Nov 14 '18 at 20:52
Console programs are similar. Instead of
WM_Close
it gets a control signal. Windows creates a thread that callsExitProcess
(programs can change this). See docs.microsoft.com/en-us/windows/console/…. Note there are only two ways to kill a program, ask it to close or destroy it. WMIC always destroys. In Task Manager (Win 7 here) the Application tab asks programs to close and the Process tab destroys applications.– CatCat
Nov 14 '18 at 21:51
Also see docs.microsoft.com/en-us/windows/desktop/api/processthreadsapi/… and docs.microsoft.com/en-us/windows/desktop/winmsg/wm-close
– CatCat
Nov 14 '18 at 21:51