How to kill Python interpreter from the command line?

Jean-Paul Calderone exarkun at divmod.com
Thu May 8 11:50:42 EDT 2008


On Thu, 8 May 2008 08:29:33 -0700 (PDT), spectrumdt at gmail.com wrote:
>Hello.
>
>I am running Fedora Linux and KDE, using the Konsole command line.
>
>When coding Python, I regularly make a bug causing my program to not
>terminate. But how do I kill the non-terminating Python interpreter
>without killing the entire Konsole?
>
>The default way of killing the current process on the command line is
>Ctrl+C, but that doesn't work with Python. Neither do the "terminate
>task", "suspend task" or "interrupt task" commands (available from
>right-click in Konsole).

Ctrl+C often works with Python, but as with any language, it's possible
to write a program which will not respond to it.  You can use Ctrl+\
instead (Ctrl+C sends SIGINT which can be masked or otherwise ignored,
Ctrl+\ sends SIGQUIT which typically isn't) or you can use the kill
command.  You can either use it in another terminal or you can suspend
the Python process (Ctrl+Z which sends SIGSTOP and _cannot_ be masked
or otherwise ignored) and then use kill in that terminal.  You need to
know the PID or jobspec in order to use kill.  In bash (and probably
elsewhere), jobspecs are easy.  When you hit Ctrl+Z, the jobspec will
be written to your terminal in the form "[N]+ Stopped       python". "N"
is the jobspec and you can kill it with "kill %N".  You may need to use
SIGKILL to end the process, in which case use "kill -KILL %N".  You can
then resume the Python process with the "fg" command, it will immediately
end since it has be killed.

This is more a Linux/bash/etc question than a Python question, so followups
might be directed to a more appropriate forum (eg, the man page for your
shell, or a bash user group, etc).

Jean-Paul



More information about the Python-list mailing list