How to kill a running iPython process

LWZ

I'm using iPython to control some other equipment connected to my Mac. Most of the time it runs fine, but sometimes I gave a wrong command to the equipment, iPython is just hanging forever since it's waiting for a response but won't get one, so I need a way to kill the iPython process. ctrl+c doesn't do anything, it just prints a ^C string on the screen. ctrl+z can get me out of iPython, but it doesn't seem to kill it, because when I restart iPython I can't re-establish the communication with the equipment. Eventually I had to restart my computer to get the two talking again.

abarnert

ctrl+z can get me out of iPython, but it doesn't seem to kill it, because when I restart iPython I can't re-establish the communication with the equipment. Eventually I had to restart my computer to get the two talking again.

Assuming you're using bash (the default shell for OS X), the ^Z turns it into a suspended background job, and you can use job control to kill it:

$ ipython
Python 2.7.5 (default, Mar  9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
IPython 2.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:
[2]+  Stopped                 ipython
$ kill %2

[2]+  Stopped                 ipython
$

[2]+  Terminated: 15          ipython
$

A couple of things to notice there:

  • Usually the backgrounded program will be job 1, but you can't rely on that. Look at the [2]+ Stopped; that tells you that it's job 2, so you need kill %2, not kill %1.
    • (I deliberately put another job in the background to make it 2 for demonstration purposes.)
    • If you're not sure which job you want to kill, use the jobs command to list them all.
    • Or you can use %ipython, which refers to the first job whose name starts with ipython.
    • Or %%, which means the "current" job—if you haven't done anything since the ^Z, that'll be ipython, but if you've done a bunch of other stuff since then and aren't sure if you affected job control, be careful.
  • Although the kill %2 kills the task immediately, bash may not recognize that until the next prompt; notice that I had to hit return an extra time before seeing the Terminated message.
    • In some situations, you'll need to use the wait command.
  • If IPython is very badly hung and not responding to signals properly, use kill -9 %2.
  • If you hit ^Z by mistake and don't want to kill the job, use fg %2 instead of kill %2 to get back into IPython.
  • If you want to let IPython run in the background while you do other things, use bg %2 instead of kill %2. (You can always fg or kill it later (or disown it, or various other things—see the linked guide.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related