简体   繁体   English

如何终止正在运行的iPython进程

[英]How to kill a running iPython process

I'm using iPython to control some other equipment connected to my Mac. 我正在使用iPython控制连接到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. 在大多数情况下,它运行良好,但有时我向设备发出了错误的命令,由于它正在等待响应但无法获得响应,因此iPython永远挂起,因此我需要一种方法来终止iPython进程。 ctrl+c doesn't do anything, it just prints a ^C string on the screen. ctrl+c不会执行任何操作,它只会在屏幕上打印一个^C字符串。 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. ctrl+z可以使我脱离iPython,但似乎并不能杀死它,因为当我重新启动iPython时,无法与设备重新建立通信。 Eventually I had to restart my computer to get the two talking again. 最终,我不得不重新启动计算机,以使两者再次通话。

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. ctrl + z可以使我脱离iPython,但似乎并不能杀死它,因为当我重新启动iPython时,无法与设备重新建立通信。 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: 假设您使用的是bash (OS X的默认外壳),^ Z会将其变成挂起的后台作业,您可以使用作业控制将其杀死:

$ 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. 通常,后台程序将是作业1,但是您不能依赖它。 Look at the [2]+ Stopped ; [2]+ Stopped ; that tells you that it's job 2, so you need kill %2 , not kill %1 . 告诉您这是工作2,因此您需要kill %2而不是kill %1
    • (I deliberately put another job in the background to make it 2 for demonstration purposes.) (我特意在后台放置了另一项工作,使其成为2用于演示目的。)
    • If you're not sure which job you want to kill, use the jobs command to list them all. 如果不确定要杀死哪个作业,请使用jobs命令列出所有jobs
    • Or you can use %ipython , which refers to the first job whose name starts with ipython . 或者,您可以使用%ipython ,这是指第一个名称以ipython开头的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. %% ,这意味着“当前”工作-如果自^Z以来ipython任何事情,那将是ipython ,但如果ipython您做了很多其他工作,并且不确定是否影响工作控制,要小心。
  • Although the kill %2 kills the task immediately, bash may not recognize that until the next prompt; 尽管kill %2立即bash了该任务,但bash可能直到下一个提示时才意识到这一点; notice that I had to hit return an extra time before seeing the Terminated message. 请注意,在看到“已Terminated消息之前,我必须打return额外的时间。
    • In some situations, you'll need to use the wait command. 在某些情况下,您需要使用wait命令。
  • If IPython is very badly hung and not responding to signals properly, use kill -9 %2 . 如果IPython严重挂起并且无法正确响应信号,请使用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 . 如果您误击了^Z并且不想终止该作业,请使用fg %2而不是kill %2返回IPython
  • If you want to let IPython run in the background while you do other things, use bg %2 instead of kill %2 . 如果要在执行其他操作时让IPython在后台运行,请使用bg %2而不是kill %2 (You can always fg or kill it later (or disown it, or various other things—see the linked guide.) (您以后可以随时fgkill它(或将其disown或进行其他操作,请参阅链接的指南。)

In the Terminal find out the process id using the "top" command. 在终端中,使用“ top”命令查找进程ID。 It is the PID column, and under the COMMAND column you will see IPython, or something similar. 它是PID列,在COMMAND列下,您将看到IPython或类似内容。 Them run kill -9 PID 他们跑kill -9 PID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM