Linux 死机了怎么办

今天在 Linuxgem.org 上面发现一篇很有意思的文章,和大家分享。

{Via http://blog.upsuper.org.cn/what-to-do-if-linux-crash/}

Linux 会死机么?我可以很肯定地说,会!要让 Linux 死机很容易,但难的是在死机以后如何安全的让他摆脱死机状态,本文讲述如何从 Linux 的死机状态中挣脱出来。

Linux 死机有很多种情况,最常见的是系统负载过高导致的。如上次介绍的 fork 炸弹就是这个原理,此外还可以运行内存耗用极大的程序(如虚拟机),也会迅速提升系统负载。由于系统负载过高导致的卡死,一定是解决的越快越好!此时必须记住的是,不能再试图依赖任何图形界面的东西,如 Gnome 的系统监视器(这是我从 Windows 遗留下来的愚昧习惯……),这只会继续加重这种卡死的局面。那怎么办?

不要怕,Linux 最初就是不需要图形界面的,因为有一个很强大的文字界面。按 Ctrl-Alt-F1(F1-F6 一般来说都可以),然后等一会儿,就会切换到 tty,也就是所谓的文字界面。这个时候需要用用户名密码登入。注意,可能键盘输入的速度比较慢,不过应该还是可以忍受的。下面在提示符后面输入 top 回车,这时会看到一张动态的表,上面列出了耗用资源最多的进程。观察它刷新一两次,按q退出,然后输入 kill ,其中的 PID 你可以在 top 里面看到。这个时候应该会快了不少,如果你发现没有成功结束掉,就再输入 kill -KILL ,这次基本上就没问题了。

除了上面这种情况外,一些底层软件的 bug 也可能导致一些奇怪的死机问题。我某位同学近日就因为莫名其妙的死机强制重启把 ext4 的分区给伤了。(最后是 sysreccd 修复了,貌似是分区表部分损坏)据其说,死机是键鼠均无反应。一般来说,系统负载高导致的死机,在可怕键盘也会有反应,有人说如果死机到键盘无响应“负载都无限大了”,亦有人说这可能是 X Server 与驱动配合有问题导致的。不过我们考虑一下遇到这种情况怎么办?

OK,这是今天才学到的方法,叫做 reisub,这个方法可以在各种情况下安全地重启计算机。大家在键盘上找,可以找到一个叫做“Sys Rq”的键,在台机的键盘上通常与 Prt Sc 共键,在笔记本可能在其他位置,如 Delete。以台机为例,要使用这种方法需要按住 Alt-Print(Sys Rq),然后依次按下 reisub 这几个键,按完 b 系统就会重启。

下面解释一下这个方法:其实 Sys Rq 是一种叫做系统请求的东西,按住 Alt-Print 的时候就相当于按住了 Sys Rq 键,这个时候输入的一切都会直接由 Linux 内核来处理,它可以进行许多低级操作。这个时候 reisub 中的每一个字母都是一个独立操作,他们分别表示:

unRaw 将键盘控制从 X Server 那里抢回来
tErminate 给所有进程发送 SIGTERM 信号,让他们自己解决善后
kIll 给所有进程发送 SIGKILL 信号,强制他们马上关闭
Sync 将所有数据同步至磁盘
Unmount 将所有分区挂载为只读模式
reBoot 重启

这6个字母的顺序是不可以记错的。那怎么记呢?这里提供一个个人认为比较好的方法:单词 busier(busy 的比较级,更忙)倒过来就是了。

基本上就这样了,Linux 的死机本来就不多见,现在见了也可以安全解决,多好~

Read More:

62 Comments

  1. 1 eric Commented @ 2009-09-03 0:46Reply to this comment

    不错,回去试试。

  2. 2 rayman Commented @ 2009-09-03 1:03Reply to this comment

    reisub,第一次听说,找个机会试试。

  3. 3 foolz Commented @ 2009-09-03 1:50Reply to this comment

    机会难求

  4. 4 GraphiteCube Commented @ 2009-09-03 2:26Reply to this comment

    難得的教學, 謝謝. :)

  5. 5 techlive Commented @ 2009-09-03 2:32Reply to this comment

    哈哈,第一个方法我倒是常用的。。。 不过我习惯用 kill -9 PID 来做。。 后面这个的确很有帮助,等我有机会试下,话说,确实碰到国好几次这种情况,有几次是因为使用ati私有驱动造成的,其他记不太清了,不过大部分和 xorg 有关。。也是什么都没用的那种。。 只有电源重启了

  6. 6 techlive Commented @ 2009-09-03 3:11Reply to this comment

    额,貌似没用。。。

  7. 7 Gcell Commented @ 2009-09-03 4:03Reply to this comment

    rawhide必备,小白必备,高手更必备,居家旅行必备良器……

  8. 8 spartan Commented @ 2009-09-03 7:37Reply to this comment

    我马上就试,我的ubuntu老死机,可能是硬件的问题,希望能用,省的老是硬重启!!

  9. 9 SaltyEgg Commented @ 2009-09-03 7:58Reply to this comment

    reisub 不好用啊。。不知道是不是我的操作有问题。。有什么注意事项吗?我的笔记本 SysRq 是 Fn + PrtSc,结果按下以后是抓图。。。

  10. 10 liyanrui Commented @ 2009-09-03 8:07Reply to this comment

    @ SaltyEgg: 估计是你的 kernel 没有开启这个功能。

  11. 11 Iven Commented @ 2009-09-03 8:09Reply to this comment

    我怎么记得是 rseiub……

  12. 12 eagle Commented @ 2009-09-03 8:26Reply to this comment

    死机键盘能动还是可以搞定,有几次碰到键盘挂了,鼠标还活着,感觉是compiz与某些什么东东搞的。键盘挂了还能搞定?上面方法?

  13. 13 skywayman Commented @ 2009-09-03 8:45Reply to this comment

    哥哥,图形界面是抓图啊,怎么搞...

  14. 14 liu mingwei Commented @ 2009-09-03 8:45Reply to this comment

    如果CTRL+ALI+F1...F12都没有反应,Alt-Print(Sys Rq)会有效?

  15. 15 snail Commented @ 2009-09-03 9:01Reply to this comment

    作者这么说的就是X死了的情况吧,如果是机器真的死了呢,我可是死过几次,死掉死键盘根本无法操作,只能掉电。

  16. 16 majia321 Commented @ 2009-09-03 9:05Reply to this comment

    @snail: me too. 我这死机时键盘根本不响应,只能强制断电。

  17. 17 sqbing Commented @ 2009-09-03 9:36Reply to this comment

    resuib这个方法管用?? 我升级内核之后,经常这样无法正常关机,试试下次

  18. 18 huangda1982 Commented @ 2009-09-03 9:39Reply to this comment

    试了一下,好像按下Alt+SysRq后Alt不能松开。 比如Reisub,就是ALT按下然后依次按SysRq R SysRq e SysRq i SysRq s SysRq u SysRq b。而且据说有按照某个操作耗时多少留出一定时间。比如S和U留个20几秒啥的。

  19. 19 huangda1982 Commented @ 2009-09-03 9:41Reply to this comment

    上面的写错了,不用按那么多次的SysRq的。

  20. 20 Hedan Commented @ 2009-09-03 9:57Reply to this comment

    Raising Skinny Elephants Is Utterly Boring

  21. 21 upsuper Commented @ 2009-09-03 10:00Reply to this comment

    我的文章被绕了一圈转过来了…… 我当初是不是应该直接来这里投递?

  22. 22 黑日白月 Commented @ 2009-09-03 10:02Reply to this comment

    呃……其实我的问题是……本本的 BIOS APM 模块不支持重启……只能关机……

    那么最后一个命令应该改变成什么呢?

  23. 23 Iron_Feet Commented @ 2009-09-03 10:03Reply to this comment

    resuib的记法倒是很有趣!

  24. 24 Iron_Feet Commented @ 2009-09-03 10:03Reply to this comment

    @Iron_Feet: reisub 呃打错了

  25. 25 upsuper Commented @ 2009-09-03 10:04Reply to this comment

    首先是这个几大发行版只有Ubuntu默认开启了,其他发行版需要手动开启这项配置。然后那个Alt+Prt Sc是要按住再按其他键,或者说其实都是三个键三个键按的,只不过其中两个键总是一样的而已。

  26. 26 upsuper Commented @ 2009-09-03 10:06Reply to this comment

    @黑日白月:理论上说是o,这个维基百科上有相关记载:http://en.wikipedia.org/wiki/Magic_SysRq_key

  27. 27 leemon Commented @ 2009-09-03 10:12Reply to this comment

    图形界面下是截图,试验了一下,得先login控制台,然后操作有效。可是如果能进控制台,也不用重启呀。

  28. 28 黑日白月 Commented @ 2009-09-03 10:13Reply to this comment

    @upsuper:

    找到了,呵呵~ http://en.wikipedia.org/wiki/Magic_SysRq_key

  29. 29 yetiboy Commented @ 2009-09-03 10:13Reply to this comment

    cat /proc/sys/kernel/sysrq 可以看看是否开启 在 /etc/sysclt.conf中: kernel.sysrq = 1 这样就可以默认启动开启了

    sys rq按了后,alt要一直按住再依次按reisub

  30. 30 Skyrunner Commented @ 2009-09-03 10:16Reply to this comment

    Alt键需要一直按着不放, Ubuntu 9.04试验成功.

  31. 31 xLight Commented @ 2009-09-03 10:17Reply to this comment

    如果是线上服务器,我们只能ssh登陆的情况呢?这个也好用吗? 恐怕要用控制卡了吧啊?

  32. 32 Skyrunner Commented @ 2009-09-03 10:18Reply to this comment

    @leemon: 把截图的快捷键改下就行了

  33. 33 RRG Commented @ 2009-09-03 10:20Reply to this comment

    这是怎么回事?

    # cat /etc/sysctl.conf|grep sysrq
    # cat /proc/sys/kernel/sysrq 
    1
    
  34. 34 RRG Commented @ 2009-09-03 10:21Reply to this comment

    cat /etc/sysctl.conf|grep sysrq 无回显 cat /proc/sys/kernel/sysrq 1 系统是debian

  35. 35 sareil Commented @ 2009-09-03 11:46Reply to this comment

    最近玩虚拟机死机多次,现在有救了

  36. 36 rhfcaesar Commented @ 2009-09-03 11:53Reply to this comment

    就怕键盘、鼠标统统不灵。

  37. 37 koalaz Commented @ 2009-09-03 12:54Reply to this comment

    cat /proc/sys/kernel/sysrq 结果是0,怎么试都没用。 用echo 1 > /proc/sys/kernel/sysrq 打开后再试果然可以了。 我的系统是FC8,那我想如果系统已经卡住了,但sysrq为0怎么办?

  38. 38 npnufn Commented @ 2009-09-03 13:39Reply to this comment

    按住 Alt-Print(Sys Rq)后,如果没什么重要数据要保存,直接按b重启动不可以么?

  39. 39 pjq Commented @ 2009-09-03 14:00Reply to this comment

    有时,终端都切换不了,那就真的没救了。

  40. 40 upsuper Commented @ 2009-09-03 14:02Reply to this comment

    @xLight: 线上服务器无效,只能接触物理键盘,不然安全性大打折扣啊

    @koalaz: 在开机脚本里面让其开机自动设置

    @npnufn: 你没有但系统可能有啊

  41. 41 upsuper Commented @ 2009-09-03 14:03Reply to this comment

    @pjq: 终端切换不了sysrq仍然可以用,我试过了

  42. 42 upsuper Commented @ 2009-09-03 14:04Reply to this comment

    @leemon: 要按住

  43. 43 yetiboy Commented @ 2009-09-03 14:19Reply to this comment

    @koalaz:编辑/etc/sysclt.conf,添加 kernel.sysrq = 1 就行了 :)

  44. 44 木木 Commented @ 2009-09-03 15:00Reply to this comment

    最近linux老死机,mandriva和ubuntu都会,都不想用了……

  45. 45 逸飞 Commented @ 2009-09-03 15:42Reply to this comment

    前几天刚装了台ubuntu服务器。刚好有个同事的电脑坏了,就装了个WinXP的虚拟机先给她用着,下班还好好的,第二天早上上班就发现已经死机了。昨天更是死机两次,在处理doc文档时死掉。谁说“linux死机本来就不多见“?

    还好可以ssh上去,一看是Xorg导致CPU负载过高,鼠标还能动,键盘完全失去响应,根本不可能转到其它tty去。但是远程连接虚拟机运行还是正常,速度也快,就是本地显示不动了哇。

    更可恶的是,那个Xorg是怎么杀都杀不死! 重启gdm也没用!google了一下,有人说是gnome 2.26.3导至Xorg的CPU占用过高, 我就把gdm换成xdm,Xorg也是照死不误呀。

  46. 46 逸飞 Commented @ 2009-09-03 15:44Reply to this comment

    开启了CTL+ALT+backspace也没用,根本就不理你。气死我了。

  47. 47 逸飞 Commented @ 2009-09-03 15:53Reply to this comment

    以前linuxer天天笑win的蓝屏,看来现win可以来笑linux的死机了。

  48. 48 lxfancy Commented @ 2009-09-03 16:00Reply to this comment

    看我的总结; http://lxfancy.is-programmer.com/posts/10826.html

  49. 49 黑日白月 Commented @ 2009-09-03 18:07Reply to this comment

    @逸飞:

    估计是虚拟机配置不正确的问题吧……

    况且,这个是 Xorg 的问题,用本文的 Kernel Debug 方式,也是可以解决的。

    GNOME 2.26.3 没有问题的,我就在用,没有导致 Xorg CPU 占有过高的情况。

    @逸飞:

    Xorg 可不是只有 Linux 在用的…… 其他的 *nix 系统的 X 系统也都是用它的……

  50. 50 RRG Commented @ 2009-09-03 22:23Reply to this comment

    @leemon: 按Win+Alt+Print不放,然后reisub

  51. 51 djstava Commented @ 2009-09-04 9:02Reply to this comment

    不开桌面的话,有这么多问题吗?我倒是没怎么遇过。

  52. 52 ccaaatt Commented @ 2009-09-04 9:22Reply to this comment

    我们不是基诺李维那样的牛人,能从ascii流里读出一个超4维世界,

    没桌面还真干不来像看电影,写文档,读电路图,上网之类没有技术含量的事

  53. 53 逸飞 Commented @ 2009-09-04 11:35Reply to this comment

    @黑日白月: 我的虚拟机用的是用KVM -daemonize -nographic开启,再用rdesktop连接上去的。 不知道有什么地方配置不对。在我的笔记本上用这种方法很久了都没有死机,不知道为什么新机上老死机。 昨天没天X,到现在也没事。

  54. 54 weff Commented @ 2009-09-04 14:30Reply to this comment

    靠,真的耶!我在VBox上成功,台式机 按住[ALT]+[印屏幕PrScrn系统请求SysRq]不要松 再依次键入reisub这6个字母键

  55. 55 weff Commented @ 2009-09-04 14:32Reply to this comment

    我还在聊QQ,这样做之后在毫无提示的情况下重启了,像按了机箱上的重启键

  56. 56 Rheometer Commented @ 2009-09-04 17:50Reply to this comment

    学习一下,还真不知道这个东西,只会切换到tty

  57. 57 wwq0327 Commented @ 2009-09-04 21:11Reply to this comment

    实在是经典,学习并收藏了。

  58. 58 自由建客 Commented @ 2009-09-04 22:15Reply to this comment

    我的本,Fn 居左下,SysRq 和 Del 合并居右上,两只手很难操作

  59. 59 lol Commented @ 2009-09-13 17:50Reply to this comment

    reisub = "Reboot Even If System Utterly Broken"

  60. 60 ubuntu lover Commented @ 2009-09-23 22:49Reply to this comment

    把萤幕截图改到别键了, 真的有用, 谢谢分享!

  61. 61 yjwork Commented @ 2009-12-29 21:32Reply to this comment

    试了下,刚输入到er,后面的还没输X就挂了 然后退回TTY1,登录后再SLIM和startx都是进入图形就死机

  62. 62 yjwork Commented @ 2009-12-29 21:40Reply to this comment

    原来我搞错了