Terminating a Process from CMD: Softest to Hardest










1















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.










share|improve this question






















  • 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 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















1















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.










share|improve this question






















  • 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 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













1












1








1








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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











  • 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 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 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
















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












1 Answer
1






active

oldest

votes


















0














As CatCat mentioned, there are two main ways to terminate a process : WM_CLOSE and TerminateProcess(). I've included two more for completeness sake.




  1. Sending window message WM_CLOSE to the main window of the process. This is the same message an application receives when user clicks X 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




  2. There is also WM_ENDSESSION message - it it sent by the OS when shutting down (after WM_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.




  3. 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.




  4. TerminateProcess() tells the OS to forcefully terminate the process. This is what happens when you click End process button on processes tab in the task 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. Both wmic process call terminate and wmic process delete appear to also do this although I'm not sure.







share|improve this answer






















    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
    );



    );













    draft saved

    draft discarded


















    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









    0














    As CatCat mentioned, there are two main ways to terminate a process : WM_CLOSE and TerminateProcess(). I've included two more for completeness sake.




    1. Sending window message WM_CLOSE to the main window of the process. This is the same message an application receives when user clicks X 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




    2. There is also WM_ENDSESSION message - it it sent by the OS when shutting down (after WM_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.




    3. 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.




    4. TerminateProcess() tells the OS to forcefully terminate the process. This is what happens when you click End process button on processes tab in the task 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. Both wmic process call terminate and wmic process delete appear to also do this although I'm not sure.







    share|improve this answer



























      0














      As CatCat mentioned, there are two main ways to terminate a process : WM_CLOSE and TerminateProcess(). I've included two more for completeness sake.




      1. Sending window message WM_CLOSE to the main window of the process. This is the same message an application receives when user clicks X 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




      2. There is also WM_ENDSESSION message - it it sent by the OS when shutting down (after WM_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.




      3. 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.




      4. TerminateProcess() tells the OS to forcefully terminate the process. This is what happens when you click End process button on processes tab in the task 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. Both wmic process call terminate and wmic process delete appear to also do this although I'm not sure.







      share|improve this answer

























        0












        0








        0







        As CatCat mentioned, there are two main ways to terminate a process : WM_CLOSE and TerminateProcess(). I've included two more for completeness sake.




        1. Sending window message WM_CLOSE to the main window of the process. This is the same message an application receives when user clicks X 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




        2. There is also WM_ENDSESSION message - it it sent by the OS when shutting down (after WM_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.




        3. 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.




        4. TerminateProcess() tells the OS to forcefully terminate the process. This is what happens when you click End process button on processes tab in the task 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. Both wmic process call terminate and wmic process delete appear to also do this although I'm not sure.







        share|improve this answer













        As CatCat mentioned, there are two main ways to terminate a process : WM_CLOSE and TerminateProcess(). I've included two more for completeness sake.




        1. Sending window message WM_CLOSE to the main window of the process. This is the same message an application receives when user clicks X 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




        2. There is also WM_ENDSESSION message - it it sent by the OS when shutting down (after WM_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.




        3. 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.




        4. TerminateProcess() tells the OS to forcefully terminate the process. This is what happens when you click End process button on processes tab in the task 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. Both wmic process call terminate and wmic process delete appear to also do this although I'm not sure.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 22:24









        Jack WhiteJack White

        33416




        33416





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo