How to stop/terminate a python script from running?












28















I wrote a program in IDLE to tokenize text files and it starts to tokeniza 349 text files! How can I stop it? How can I stop a running Python program?










share|improve this question




















  • 17





    Press the power button! Unplug the power cord! Quickly before it becomes sentient!!

    – Thomas
    Nov 5 '13 at 4:48






  • 3





    ctrl+c should kill it. Alternatively, kill -9 it

    – xbonez
    Nov 5 '13 at 4:48






  • 1





    A python script slithers!

    – aIKid
    Nov 5 '13 at 9:19
















28















I wrote a program in IDLE to tokenize text files and it starts to tokeniza 349 text files! How can I stop it? How can I stop a running Python program?










share|improve this question




















  • 17





    Press the power button! Unplug the power cord! Quickly before it becomes sentient!!

    – Thomas
    Nov 5 '13 at 4:48






  • 3





    ctrl+c should kill it. Alternatively, kill -9 it

    – xbonez
    Nov 5 '13 at 4:48






  • 1





    A python script slithers!

    – aIKid
    Nov 5 '13 at 9:19














28












28








28


4






I wrote a program in IDLE to tokenize text files and it starts to tokeniza 349 text files! How can I stop it? How can I stop a running Python program?










share|improve this question
















I wrote a program in IDLE to tokenize text files and it starts to tokeniza 349 text files! How can I stop it? How can I stop a running Python program?







python execution terminate termination






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 17 '16 at 19:38









Jack Bracken

829920




829920










asked Nov 5 '13 at 4:46









MACEEMACEE

190348




190348








  • 17





    Press the power button! Unplug the power cord! Quickly before it becomes sentient!!

    – Thomas
    Nov 5 '13 at 4:48






  • 3





    ctrl+c should kill it. Alternatively, kill -9 it

    – xbonez
    Nov 5 '13 at 4:48






  • 1





    A python script slithers!

    – aIKid
    Nov 5 '13 at 9:19














  • 17





    Press the power button! Unplug the power cord! Quickly before it becomes sentient!!

    – Thomas
    Nov 5 '13 at 4:48






  • 3





    ctrl+c should kill it. Alternatively, kill -9 it

    – xbonez
    Nov 5 '13 at 4:48






  • 1





    A python script slithers!

    – aIKid
    Nov 5 '13 at 9:19








17




17





Press the power button! Unplug the power cord! Quickly before it becomes sentient!!

– Thomas
Nov 5 '13 at 4:48





Press the power button! Unplug the power cord! Quickly before it becomes sentient!!

– Thomas
Nov 5 '13 at 4:48




3




3





ctrl+c should kill it. Alternatively, kill -9 it

– xbonez
Nov 5 '13 at 4:48





ctrl+c should kill it. Alternatively, kill -9 it

– xbonez
Nov 5 '13 at 4:48




1




1





A python script slithers!

– aIKid
Nov 5 '13 at 9:19





A python script slithers!

– aIKid
Nov 5 '13 at 9:19












12 Answers
12






active

oldest

votes


















23














To stop your program, just press Control + C.






share|improve this answer





















  • 11





    This method worked on my windows laptop, but on my linux desktop, it showed ^C in the terminal and the system monitor showed python is still using a lot of CPU...

    – user3768495
    Nov 29 '15 at 17:15











  • Is it possible to resume the script after stopping it?

    – Gathide
    Sep 7 '18 at 12:09



















19














You can also do it if you use the exit() function in your code. More ideally, you can do sys.exit(). sys.exit() might terminate Python even if you are running things in parallel through the multiprocessing package.






share|improve this answer


























  • Note: I could find examples in which sys.exit(1) doesn't stop the process. I basically have multiple threads and each of them blocks on external processes started by Popen. I need a nuclear option to kill all sub-processes created by the Python process as well as the Python process itself. Didn't find so far.

    – Dici
    Feb 8 at 21:32



















15














Ctrl-Break it is more powerful than Ctrl-C






share|improve this answer
























  • Ctrl-Break worked for me on Windows 10!!

    – Arun
    Apr 30 '18 at 12:01











  • Like Daniel Pryden clarifies in his answer, the Break key on the keyboard could also be labelled Pause, for those who were confused like me. :)

    – JakeStrang
    Feb 5 at 20:23



















12















  • To stop a python script just press Ctrl + C.

  • Inside a script with exit(), you can do it.

  • You can do it in an interactive script with just exit.

  • You can use pkill -f name-of-the-python-script.






share|improve this answer

































    7














    If your program is running at an interactive console, pressing CTRL + C will raise a KeyboardInterrupt exception on the main thread.



    If your Python program doesn't catch it, the KeyboardInterrupt will cause Python to exit. However, an except KeyboardInterrupt: block, or something like a bare except:, will prevent this mechanism from actually stopping the script from running.



    Sometimes if KeyboardInterrupt is not working you can send a SIGBREAK signal instead; on Windows, CTRL + Pause/Break may be handled by the interpreter without generating a catchable KeyboardInterrupt exception.



    However, these mechanisms mainly only work if the Python interpreter is running and responding to operating system events. If the Python interpreter is not responding for some reason, the most effective way is to terminate the entire operating system process that is running the interpreter. The mechanism for this varies by operating system.



    In a Unix-style shell environment, you can press CTRL + Z to suspend whatever process is currently controlling the console. Once you get the shell prompt back, you can use jobs to list suspended jobs, and you can kill the first suspended job with kill %1. (If you want to start it running again, you can continue the job in the foreground by using fg %1; read your shell's manual on job control for more information.)



    Alternatively, in a Unix or Unix-like environment, you can find the Python process's PID (process identifier) and kill it by PID. Use something like ps aux | grep python to find which Python processes are running, and then use kill <pid> to send a SIGTERM signal.



    The kill command on Unix sends SIGTERM by default, and a Python program can install a signal handler for SIGTERM using the signal module. In theory, any signal handler for SIGTERM should shut down the process gracefully. But sometimes if the process is stuck (for example, blocked in an uninterruptable IO sleep state), a SIGTERM signal has no effect because the process can't even wake up to handle it.



    To forcibly kill a process that isn't responding to signals, you need to send the SIGKILL signal, sometimes referred to as kill -9 because 9 is the numeric value of the SIGKILL constant. From the command line, you can use kill -KILL <pid> (or kill -9 <pid> for short) to send a SIGKILL and stop the process running immediately.



    On Windows, you don't have the Unix system of process signals, but you can forcibly terminate a running process by using the TerminateProcess function. Interactively, the easiest way to do this is to open Task Manager, find the python.exe process that corresponds to your program, and click the "End Process" button. You can also use the taskkill command for similar purposes.






    share|improve this answer































      4














      To stop a the running program: use CTRL+C to terminate the process.



      To handle it programmatically in python: import the sys module and use sys.exit where you want to terminate the program.



          import sys
      sys.exit()





      share|improve this answer































        2














        you can also use the Activity Monitor to stop the py process






        share|improve this answer































          1














          When I have a python script running on a linux terminal, CTRL + works. (not CRTL + C or D)






          share|improve this answer































            0














            To stop your program, just press CTRL + D



            or exit().






            share|improve this answer

































              0














              Ctrl + Z should do it, if you're caught in the python shell. Keep in mind that instances of the script could continue running in background, so under linux you have to kill the corresponding process.






              share|improve this answer































                0














                Press Ctrl+Alt+Delete and Task Manager will pop up. Find the Python command running, right click on it and and click Stop or Kill.






                share|improve this answer

































                  -1














                  Control-D works for me on Windows 10. Also, putting exit() at the end also works.






                  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%2f19782075%2fhow-to-stop-terminate-a-python-script-from-running%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    12 Answers
                    12






                    active

                    oldest

                    votes








                    12 Answers
                    12






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    23














                    To stop your program, just press Control + C.






                    share|improve this answer





















                    • 11





                      This method worked on my windows laptop, but on my linux desktop, it showed ^C in the terminal and the system monitor showed python is still using a lot of CPU...

                      – user3768495
                      Nov 29 '15 at 17:15











                    • Is it possible to resume the script after stopping it?

                      – Gathide
                      Sep 7 '18 at 12:09
















                    23














                    To stop your program, just press Control + C.






                    share|improve this answer





















                    • 11





                      This method worked on my windows laptop, but on my linux desktop, it showed ^C in the terminal and the system monitor showed python is still using a lot of CPU...

                      – user3768495
                      Nov 29 '15 at 17:15











                    • Is it possible to resume the script after stopping it?

                      – Gathide
                      Sep 7 '18 at 12:09














                    23












                    23








                    23







                    To stop your program, just press Control + C.






                    share|improve this answer















                    To stop your program, just press Control + C.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 14 '14 at 4:24

























                    answered Nov 5 '13 at 4:49









                    ChristianChristian

                    27.3k43159




                    27.3k43159








                    • 11





                      This method worked on my windows laptop, but on my linux desktop, it showed ^C in the terminal and the system monitor showed python is still using a lot of CPU...

                      – user3768495
                      Nov 29 '15 at 17:15











                    • Is it possible to resume the script after stopping it?

                      – Gathide
                      Sep 7 '18 at 12:09














                    • 11





                      This method worked on my windows laptop, but on my linux desktop, it showed ^C in the terminal and the system monitor showed python is still using a lot of CPU...

                      – user3768495
                      Nov 29 '15 at 17:15











                    • Is it possible to resume the script after stopping it?

                      – Gathide
                      Sep 7 '18 at 12:09








                    11




                    11





                    This method worked on my windows laptop, but on my linux desktop, it showed ^C in the terminal and the system monitor showed python is still using a lot of CPU...

                    – user3768495
                    Nov 29 '15 at 17:15





                    This method worked on my windows laptop, but on my linux desktop, it showed ^C in the terminal and the system monitor showed python is still using a lot of CPU...

                    – user3768495
                    Nov 29 '15 at 17:15













                    Is it possible to resume the script after stopping it?

                    – Gathide
                    Sep 7 '18 at 12:09





                    Is it possible to resume the script after stopping it?

                    – Gathide
                    Sep 7 '18 at 12:09













                    19














                    You can also do it if you use the exit() function in your code. More ideally, you can do sys.exit(). sys.exit() might terminate Python even if you are running things in parallel through the multiprocessing package.






                    share|improve this answer


























                    • Note: I could find examples in which sys.exit(1) doesn't stop the process. I basically have multiple threads and each of them blocks on external processes started by Popen. I need a nuclear option to kill all sub-processes created by the Python process as well as the Python process itself. Didn't find so far.

                      – Dici
                      Feb 8 at 21:32
















                    19














                    You can also do it if you use the exit() function in your code. More ideally, you can do sys.exit(). sys.exit() might terminate Python even if you are running things in parallel through the multiprocessing package.






                    share|improve this answer


























                    • Note: I could find examples in which sys.exit(1) doesn't stop the process. I basically have multiple threads and each of them blocks on external processes started by Popen. I need a nuclear option to kill all sub-processes created by the Python process as well as the Python process itself. Didn't find so far.

                      – Dici
                      Feb 8 at 21:32














                    19












                    19








                    19







                    You can also do it if you use the exit() function in your code. More ideally, you can do sys.exit(). sys.exit() might terminate Python even if you are running things in parallel through the multiprocessing package.






                    share|improve this answer















                    You can also do it if you use the exit() function in your code. More ideally, you can do sys.exit(). sys.exit() might terminate Python even if you are running things in parallel through the multiprocessing package.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Aug 25 '18 at 17:28

























                    answered Dec 1 '15 at 20:27









                    AmirAmir

                    4,83242649




                    4,83242649













                    • Note: I could find examples in which sys.exit(1) doesn't stop the process. I basically have multiple threads and each of them blocks on external processes started by Popen. I need a nuclear option to kill all sub-processes created by the Python process as well as the Python process itself. Didn't find so far.

                      – Dici
                      Feb 8 at 21:32



















                    • Note: I could find examples in which sys.exit(1) doesn't stop the process. I basically have multiple threads and each of them blocks on external processes started by Popen. I need a nuclear option to kill all sub-processes created by the Python process as well as the Python process itself. Didn't find so far.

                      – Dici
                      Feb 8 at 21:32

















                    Note: I could find examples in which sys.exit(1) doesn't stop the process. I basically have multiple threads and each of them blocks on external processes started by Popen. I need a nuclear option to kill all sub-processes created by the Python process as well as the Python process itself. Didn't find so far.

                    – Dici
                    Feb 8 at 21:32





                    Note: I could find examples in which sys.exit(1) doesn't stop the process. I basically have multiple threads and each of them blocks on external processes started by Popen. I need a nuclear option to kill all sub-processes created by the Python process as well as the Python process itself. Didn't find so far.

                    – Dici
                    Feb 8 at 21:32











                    15














                    Ctrl-Break it is more powerful than Ctrl-C






                    share|improve this answer
























                    • Ctrl-Break worked for me on Windows 10!!

                      – Arun
                      Apr 30 '18 at 12:01











                    • Like Daniel Pryden clarifies in his answer, the Break key on the keyboard could also be labelled Pause, for those who were confused like me. :)

                      – JakeStrang
                      Feb 5 at 20:23
















                    15














                    Ctrl-Break it is more powerful than Ctrl-C






                    share|improve this answer
























                    • Ctrl-Break worked for me on Windows 10!!

                      – Arun
                      Apr 30 '18 at 12:01











                    • Like Daniel Pryden clarifies in his answer, the Break key on the keyboard could also be labelled Pause, for those who were confused like me. :)

                      – JakeStrang
                      Feb 5 at 20:23














                    15












                    15








                    15







                    Ctrl-Break it is more powerful than Ctrl-C






                    share|improve this answer













                    Ctrl-Break it is more powerful than Ctrl-C







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 26 '17 at 22:18









                    Scott P.Scott P.

                    40437




                    40437













                    • Ctrl-Break worked for me on Windows 10!!

                      – Arun
                      Apr 30 '18 at 12:01











                    • Like Daniel Pryden clarifies in his answer, the Break key on the keyboard could also be labelled Pause, for those who were confused like me. :)

                      – JakeStrang
                      Feb 5 at 20:23



















                    • Ctrl-Break worked for me on Windows 10!!

                      – Arun
                      Apr 30 '18 at 12:01











                    • Like Daniel Pryden clarifies in his answer, the Break key on the keyboard could also be labelled Pause, for those who were confused like me. :)

                      – JakeStrang
                      Feb 5 at 20:23

















                    Ctrl-Break worked for me on Windows 10!!

                    – Arun
                    Apr 30 '18 at 12:01





                    Ctrl-Break worked for me on Windows 10!!

                    – Arun
                    Apr 30 '18 at 12:01













                    Like Daniel Pryden clarifies in his answer, the Break key on the keyboard could also be labelled Pause, for those who were confused like me. :)

                    – JakeStrang
                    Feb 5 at 20:23





                    Like Daniel Pryden clarifies in his answer, the Break key on the keyboard could also be labelled Pause, for those who were confused like me. :)

                    – JakeStrang
                    Feb 5 at 20:23











                    12















                    • To stop a python script just press Ctrl + C.

                    • Inside a script with exit(), you can do it.

                    • You can do it in an interactive script with just exit.

                    • You can use pkill -f name-of-the-python-script.






                    share|improve this answer






























                      12















                      • To stop a python script just press Ctrl + C.

                      • Inside a script with exit(), you can do it.

                      • You can do it in an interactive script with just exit.

                      • You can use pkill -f name-of-the-python-script.






                      share|improve this answer




























                        12












                        12








                        12








                        • To stop a python script just press Ctrl + C.

                        • Inside a script with exit(), you can do it.

                        • You can do it in an interactive script with just exit.

                        • You can use pkill -f name-of-the-python-script.






                        share|improve this answer
















                        • To stop a python script just press Ctrl + C.

                        • Inside a script with exit(), you can do it.

                        • You can do it in an interactive script with just exit.

                        • You can use pkill -f name-of-the-python-script.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Jun 27 '17 at 17:57









                        Dayan

                        4,21083063




                        4,21083063










                        answered Jun 27 '17 at 17:29









                        wppwpp

                        19314




                        19314























                            7














                            If your program is running at an interactive console, pressing CTRL + C will raise a KeyboardInterrupt exception on the main thread.



                            If your Python program doesn't catch it, the KeyboardInterrupt will cause Python to exit. However, an except KeyboardInterrupt: block, or something like a bare except:, will prevent this mechanism from actually stopping the script from running.



                            Sometimes if KeyboardInterrupt is not working you can send a SIGBREAK signal instead; on Windows, CTRL + Pause/Break may be handled by the interpreter without generating a catchable KeyboardInterrupt exception.



                            However, these mechanisms mainly only work if the Python interpreter is running and responding to operating system events. If the Python interpreter is not responding for some reason, the most effective way is to terminate the entire operating system process that is running the interpreter. The mechanism for this varies by operating system.



                            In a Unix-style shell environment, you can press CTRL + Z to suspend whatever process is currently controlling the console. Once you get the shell prompt back, you can use jobs to list suspended jobs, and you can kill the first suspended job with kill %1. (If you want to start it running again, you can continue the job in the foreground by using fg %1; read your shell's manual on job control for more information.)



                            Alternatively, in a Unix or Unix-like environment, you can find the Python process's PID (process identifier) and kill it by PID. Use something like ps aux | grep python to find which Python processes are running, and then use kill <pid> to send a SIGTERM signal.



                            The kill command on Unix sends SIGTERM by default, and a Python program can install a signal handler for SIGTERM using the signal module. In theory, any signal handler for SIGTERM should shut down the process gracefully. But sometimes if the process is stuck (for example, blocked in an uninterruptable IO sleep state), a SIGTERM signal has no effect because the process can't even wake up to handle it.



                            To forcibly kill a process that isn't responding to signals, you need to send the SIGKILL signal, sometimes referred to as kill -9 because 9 is the numeric value of the SIGKILL constant. From the command line, you can use kill -KILL <pid> (or kill -9 <pid> for short) to send a SIGKILL and stop the process running immediately.



                            On Windows, you don't have the Unix system of process signals, but you can forcibly terminate a running process by using the TerminateProcess function. Interactively, the easiest way to do this is to open Task Manager, find the python.exe process that corresponds to your program, and click the "End Process" button. You can also use the taskkill command for similar purposes.






                            share|improve this answer




























                              7














                              If your program is running at an interactive console, pressing CTRL + C will raise a KeyboardInterrupt exception on the main thread.



                              If your Python program doesn't catch it, the KeyboardInterrupt will cause Python to exit. However, an except KeyboardInterrupt: block, or something like a bare except:, will prevent this mechanism from actually stopping the script from running.



                              Sometimes if KeyboardInterrupt is not working you can send a SIGBREAK signal instead; on Windows, CTRL + Pause/Break may be handled by the interpreter without generating a catchable KeyboardInterrupt exception.



                              However, these mechanisms mainly only work if the Python interpreter is running and responding to operating system events. If the Python interpreter is not responding for some reason, the most effective way is to terminate the entire operating system process that is running the interpreter. The mechanism for this varies by operating system.



                              In a Unix-style shell environment, you can press CTRL + Z to suspend whatever process is currently controlling the console. Once you get the shell prompt back, you can use jobs to list suspended jobs, and you can kill the first suspended job with kill %1. (If you want to start it running again, you can continue the job in the foreground by using fg %1; read your shell's manual on job control for more information.)



                              Alternatively, in a Unix or Unix-like environment, you can find the Python process's PID (process identifier) and kill it by PID. Use something like ps aux | grep python to find which Python processes are running, and then use kill <pid> to send a SIGTERM signal.



                              The kill command on Unix sends SIGTERM by default, and a Python program can install a signal handler for SIGTERM using the signal module. In theory, any signal handler for SIGTERM should shut down the process gracefully. But sometimes if the process is stuck (for example, blocked in an uninterruptable IO sleep state), a SIGTERM signal has no effect because the process can't even wake up to handle it.



                              To forcibly kill a process that isn't responding to signals, you need to send the SIGKILL signal, sometimes referred to as kill -9 because 9 is the numeric value of the SIGKILL constant. From the command line, you can use kill -KILL <pid> (or kill -9 <pid> for short) to send a SIGKILL and stop the process running immediately.



                              On Windows, you don't have the Unix system of process signals, but you can forcibly terminate a running process by using the TerminateProcess function. Interactively, the easiest way to do this is to open Task Manager, find the python.exe process that corresponds to your program, and click the "End Process" button. You can also use the taskkill command for similar purposes.






                              share|improve this answer


























                                7












                                7








                                7







                                If your program is running at an interactive console, pressing CTRL + C will raise a KeyboardInterrupt exception on the main thread.



                                If your Python program doesn't catch it, the KeyboardInterrupt will cause Python to exit. However, an except KeyboardInterrupt: block, or something like a bare except:, will prevent this mechanism from actually stopping the script from running.



                                Sometimes if KeyboardInterrupt is not working you can send a SIGBREAK signal instead; on Windows, CTRL + Pause/Break may be handled by the interpreter without generating a catchable KeyboardInterrupt exception.



                                However, these mechanisms mainly only work if the Python interpreter is running and responding to operating system events. If the Python interpreter is not responding for some reason, the most effective way is to terminate the entire operating system process that is running the interpreter. The mechanism for this varies by operating system.



                                In a Unix-style shell environment, you can press CTRL + Z to suspend whatever process is currently controlling the console. Once you get the shell prompt back, you can use jobs to list suspended jobs, and you can kill the first suspended job with kill %1. (If you want to start it running again, you can continue the job in the foreground by using fg %1; read your shell's manual on job control for more information.)



                                Alternatively, in a Unix or Unix-like environment, you can find the Python process's PID (process identifier) and kill it by PID. Use something like ps aux | grep python to find which Python processes are running, and then use kill <pid> to send a SIGTERM signal.



                                The kill command on Unix sends SIGTERM by default, and a Python program can install a signal handler for SIGTERM using the signal module. In theory, any signal handler for SIGTERM should shut down the process gracefully. But sometimes if the process is stuck (for example, blocked in an uninterruptable IO sleep state), a SIGTERM signal has no effect because the process can't even wake up to handle it.



                                To forcibly kill a process that isn't responding to signals, you need to send the SIGKILL signal, sometimes referred to as kill -9 because 9 is the numeric value of the SIGKILL constant. From the command line, you can use kill -KILL <pid> (or kill -9 <pid> for short) to send a SIGKILL and stop the process running immediately.



                                On Windows, you don't have the Unix system of process signals, but you can forcibly terminate a running process by using the TerminateProcess function. Interactively, the easiest way to do this is to open Task Manager, find the python.exe process that corresponds to your program, and click the "End Process" button. You can also use the taskkill command for similar purposes.






                                share|improve this answer













                                If your program is running at an interactive console, pressing CTRL + C will raise a KeyboardInterrupt exception on the main thread.



                                If your Python program doesn't catch it, the KeyboardInterrupt will cause Python to exit. However, an except KeyboardInterrupt: block, or something like a bare except:, will prevent this mechanism from actually stopping the script from running.



                                Sometimes if KeyboardInterrupt is not working you can send a SIGBREAK signal instead; on Windows, CTRL + Pause/Break may be handled by the interpreter without generating a catchable KeyboardInterrupt exception.



                                However, these mechanisms mainly only work if the Python interpreter is running and responding to operating system events. If the Python interpreter is not responding for some reason, the most effective way is to terminate the entire operating system process that is running the interpreter. The mechanism for this varies by operating system.



                                In a Unix-style shell environment, you can press CTRL + Z to suspend whatever process is currently controlling the console. Once you get the shell prompt back, you can use jobs to list suspended jobs, and you can kill the first suspended job with kill %1. (If you want to start it running again, you can continue the job in the foreground by using fg %1; read your shell's manual on job control for more information.)



                                Alternatively, in a Unix or Unix-like environment, you can find the Python process's PID (process identifier) and kill it by PID. Use something like ps aux | grep python to find which Python processes are running, and then use kill <pid> to send a SIGTERM signal.



                                The kill command on Unix sends SIGTERM by default, and a Python program can install a signal handler for SIGTERM using the signal module. In theory, any signal handler for SIGTERM should shut down the process gracefully. But sometimes if the process is stuck (for example, blocked in an uninterruptable IO sleep state), a SIGTERM signal has no effect because the process can't even wake up to handle it.



                                To forcibly kill a process that isn't responding to signals, you need to send the SIGKILL signal, sometimes referred to as kill -9 because 9 is the numeric value of the SIGKILL constant. From the command line, you can use kill -KILL <pid> (or kill -9 <pid> for short) to send a SIGKILL and stop the process running immediately.



                                On Windows, you don't have the Unix system of process signals, but you can forcibly terminate a running process by using the TerminateProcess function. Interactively, the easiest way to do this is to open Task Manager, find the python.exe process that corresponds to your program, and click the "End Process" button. You can also use the taskkill command for similar purposes.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 8 '18 at 15:44









                                Daniel PrydenDaniel Pryden

                                46.2k875117




                                46.2k875117























                                    4














                                    To stop a the running program: use CTRL+C to terminate the process.



                                    To handle it programmatically in python: import the sys module and use sys.exit where you want to terminate the program.



                                        import sys
                                    sys.exit()





                                    share|improve this answer




























                                      4














                                      To stop a the running program: use CTRL+C to terminate the process.



                                      To handle it programmatically in python: import the sys module and use sys.exit where you want to terminate the program.



                                          import sys
                                      sys.exit()





                                      share|improve this answer


























                                        4












                                        4








                                        4







                                        To stop a the running program: use CTRL+C to terminate the process.



                                        To handle it programmatically in python: import the sys module and use sys.exit where you want to terminate the program.



                                            import sys
                                        sys.exit()





                                        share|improve this answer













                                        To stop a the running program: use CTRL+C to terminate the process.



                                        To handle it programmatically in python: import the sys module and use sys.exit where you want to terminate the program.



                                            import sys
                                        sys.exit()






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Aug 25 '18 at 18:33









                                        Chandila07Chandila07

                                        507314




                                        507314























                                            2














                                            you can also use the Activity Monitor to stop the py process






                                            share|improve this answer




























                                              2














                                              you can also use the Activity Monitor to stop the py process






                                              share|improve this answer


























                                                2












                                                2








                                                2







                                                you can also use the Activity Monitor to stop the py process






                                                share|improve this answer













                                                you can also use the Activity Monitor to stop the py process







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 6 '15 at 5:48









                                                DennisDennis

                                                140214




                                                140214























                                                    1














                                                    When I have a python script running on a linux terminal, CTRL + works. (not CRTL + C or D)






                                                    share|improve this answer




























                                                      1














                                                      When I have a python script running on a linux terminal, CTRL + works. (not CRTL + C or D)






                                                      share|improve this answer


























                                                        1












                                                        1








                                                        1







                                                        When I have a python script running on a linux terminal, CTRL + works. (not CRTL + C or D)






                                                        share|improve this answer













                                                        When I have a python script running on a linux terminal, CTRL + works. (not CRTL + C or D)







                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Nov 8 '18 at 14:55









                                                        Stefun06Stefun06

                                                        162




                                                        162























                                                            0














                                                            To stop your program, just press CTRL + D



                                                            or exit().






                                                            share|improve this answer






























                                                              0














                                                              To stop your program, just press CTRL + D



                                                              or exit().






                                                              share|improve this answer




























                                                                0












                                                                0








                                                                0







                                                                To stop your program, just press CTRL + D



                                                                or exit().






                                                                share|improve this answer















                                                                To stop your program, just press CTRL + D



                                                                or exit().







                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited Jul 24 '18 at 6:25









                                                                Shree

                                                                12.7k2070122




                                                                12.7k2070122










                                                                answered Jul 24 '18 at 6:20









                                                                Vivek ParmarVivek Parmar

                                                                557724




                                                                557724























                                                                    0














                                                                    Ctrl + Z should do it, if you're caught in the python shell. Keep in mind that instances of the script could continue running in background, so under linux you have to kill the corresponding process.






                                                                    share|improve this answer




























                                                                      0














                                                                      Ctrl + Z should do it, if you're caught in the python shell. Keep in mind that instances of the script could continue running in background, so under linux you have to kill the corresponding process.






                                                                      share|improve this answer


























                                                                        0












                                                                        0








                                                                        0







                                                                        Ctrl + Z should do it, if you're caught in the python shell. Keep in mind that instances of the script could continue running in background, so under linux you have to kill the corresponding process.






                                                                        share|improve this answer













                                                                        Ctrl + Z should do it, if you're caught in the python shell. Keep in mind that instances of the script could continue running in background, so under linux you have to kill the corresponding process.







                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Aug 20 '18 at 14:20









                                                                        JukeJuke

                                                                        5281512




                                                                        5281512























                                                                            0














                                                                            Press Ctrl+Alt+Delete and Task Manager will pop up. Find the Python command running, right click on it and and click Stop or Kill.






                                                                            share|improve this answer






























                                                                              0














                                                                              Press Ctrl+Alt+Delete and Task Manager will pop up. Find the Python command running, right click on it and and click Stop or Kill.






                                                                              share|improve this answer




























                                                                                0












                                                                                0








                                                                                0







                                                                                Press Ctrl+Alt+Delete and Task Manager will pop up. Find the Python command running, right click on it and and click Stop or Kill.






                                                                                share|improve this answer















                                                                                Press Ctrl+Alt+Delete and Task Manager will pop up. Find the Python command running, right click on it and and click Stop or Kill.







                                                                                share|improve this answer














                                                                                share|improve this answer



                                                                                share|improve this answer








                                                                                edited Oct 7 '18 at 2:27









                                                                                Stephen Rauch

                                                                                29.3k153657




                                                                                29.3k153657










                                                                                answered Oct 7 '18 at 2:01









                                                                                Andrew HyderAndrew Hyder

                                                                                1




                                                                                1























                                                                                    -1














                                                                                    Control-D works for me on Windows 10. Also, putting exit() at the end also works.






                                                                                    share|improve this answer






























                                                                                      -1














                                                                                      Control-D works for me on Windows 10. Also, putting exit() at the end also works.






                                                                                      share|improve this answer




























                                                                                        -1












                                                                                        -1








                                                                                        -1







                                                                                        Control-D works for me on Windows 10. Also, putting exit() at the end also works.






                                                                                        share|improve this answer















                                                                                        Control-D works for me on Windows 10. Also, putting exit() at the end also works.







                                                                                        share|improve this answer














                                                                                        share|improve this answer



                                                                                        share|improve this answer








                                                                                        edited Jan 12 at 17:10

























                                                                                        answered Dec 31 '18 at 6:40









                                                                                        chess_lover_6chess_lover_6

                                                                                        34




                                                                                        34






























                                                                                            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%2f19782075%2fhow-to-stop-terminate-a-python-script-from-running%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

                                                                                            Monofisismo

                                                                                            Angular Downloading a file using contenturl with Basic Authentication

                                                                                            Olmecas