stop python in terminal on mac

前端 未结 2 1425
闹比i
闹比i 2021-01-03 22:19

Using python in terminal on a Mac, type

ctrl-z

will stop the python, but not exit it, giving output like this:

>>>         


        
相关标签:
2条回答
  • 2021-01-03 23:07

    This isn't a Python issue, per se. This ia a Unix shell behavior and is a difference from Windows and other systems. See the Wikipedia article on CTRL-Z for a more complete explanation:

    On Unix-like systems, Control+Z is the most common default keyboard mapping for the key sequence that suspends a process (SIGTSTP). When entered by a user at their computer terminal, the currently running foreground process is sent a SIGTSTP signal, which generally causes the process to suspend its execution. The user can later continue the process execution by typing the command 'fg' (short for foreground) or by typing 'bg' (short for background) and furthermore typing the command 'disown' to separate the background process from the terminal.

    On OS X as on various other Unix-based systems, you could use the stty command to change which key, if any, produces a SIGTSTP (or an eof, for that matter):

    $ stty -a
    speed 38400 baud; 30 rows; 90 columns;
    lflags: icanon isig iexten echo echoe echok echoke -echonl echoctl
        -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
        -extproc
    iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
        -ignbrk brkint -inpck -ignpar -parmrk
    oflags: opost onlcr -oxtabs -onocr -onlret
    cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
        -dtrflow -mdmbuf
    cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
        eol2 = <undef>; erase = ^?; intr = ^C; kill = ^U; lnext = ^V;
        min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
        stop = ^S; susp = ^Z; time = 0; werase = ^W;
    $ stty susp ^Y
    $ cat
    ^Y
    [1]+  Stopped                 cat
    
    0 讨论(0)
  • 2021-01-03 23:10

    CTRL+d -> Defines EOF (End of File).

    CTRL+c -> Will terminate most jobs.

    If, however you have written a python wrapper program that calls other python programs in turn, Ctrl-c will only stop the the job that is currently running. The wrapper program will keep running. Worst case scenario, you can do this:

    Open up: Applications -> Utilities -> Activity Monitor, Find the process labeled as python, highlight it in the activity monitor then click on "Quit Process".

    These three suggestions should work for most situations where you want a program to stop.

    0 讨论(0)
提交回复
热议问题