greatghoul / profile

学习、实践、思考、沉淀
25 stars 1 forks source link

快速杀掉失去响应 rails 进程 #34

Closed greatghoul closed 4 years ago

greatghoul commented 4 years ago

在 Ruby on Rails 的日常开发时,有时候不小心写出个坏代码(比如死循环),导致 rails dev server 进程失去响应,这时候如果想杀掉进程,按 Cmd+C 可能没有效果。

除了去 grep ps 或者查看 server.pid 外,还有一个快捷的方法快速结束进程。

按下 Cmd+Z,进程会自动转到后台运行,此时终端上会输出进程 PID

Puma starting in single mode...
* Version 3.12.0 (ruby 2.6.1-p33), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop
^Z
[1]  + 63939 suspended  rails s

kill 掉它就可以了。

$ kill -9 63939
yitong-ovo commented 4 years ago

其实 CTRL+Z 发送的是 SIGTSTP 信号,也就是挂起终端/终端停止,如果程序有相应的话说明程序对这个信号做了处理,程序并没有卡死,可能只是某个线程无法退出导致的。

如果程序没有对 SIGTSTP 做处理的话,那么这个信号也是可以忽略的(也有无效的可能性)

https://www.gnu.org/software/libc/manual/html_node/Job-Control-Signals.html https://stackoverflow.com/questions/11886812/whats-the-difference-between-sigstop-and-sigtstp

greatghoul commented 4 years ago

原来还有这样的区别,那其实遇到的卡死也不是真正意义上的卡死,只是 CTRL_C 结束不了,CTRL_Z 还能响应的情况。

@yitong-ovo