重启服务器
1. # echo 1> /proc/sys/kernel/sysrq
2. # echo b> /proc/sysrq-trigger
1. /proc/sys/kernel/sysrq
向sysrq文件中写入1是为了开启SysRq功能。根据linux/Documentations/sysrq.txt中所说:SysRq代表的是MagicSystem RequestKey。开启了这个功能以后,只要内核没有挂掉,它就会响应你要求的任何操作。但是这需要内核支持(CONFIG_MAGIC_SYSRQ选项)。向/proc/sys/kernel/sysrq中写入0是关闭sysrq功能,写入1是开启,其他选项请参考sysrq.txt。需要注意的是,/proc/sys/kernel/sysrq中的值只影响键盘的操作。
那么怎么使用SysRq键呢?
在x86平台上,组合键"<ALT> + SysRq +<commandkey>"组成SysRq键以完成各种功能。但是,在一些键盘上可能没有SysRq键。SysRq键实际上就是"PrintScreen"键。并且可能有些键盘不支持同时按三个按键,所以你可以按住"ALT键",按一下"SysRq键",再按一下"<commandkey>键",如果你运气好的话,这个会有效果的。不过放心,现在的键盘一般都支持同时按3个或3个以上的键。
<commandkey>有很多,这里只挑几个来说,其他的可以参考sysrq.txt文件。
・ 'b' ―― 将会立即重启系统,并且不会管你有没有数据没有写回磁盘,也不卸载磁盘,而是完完全全的立即关机
・ 'o' ―― 将会关机
・ 's' ―― 将会同步所有以挂在的文件系统
・ 'u' ―― 将会重新将所有的文件系统挂在为只读属性
2. /proc/sysrq-trigger
从文件名字就可以看出来这两个是有关系的。写入/proc/sysrq-trigger中的字符其实就是sysrq.txt中说的键所对应的字符,其功能也和上述一样。
所以,这两行命令先开启SysRq功能,然后用'b'命令让计算机立刻重启。
/proc/sysrq-trigger该文件能做些什么事情呢?
# 立即重新启动计算机
echo "b" > /proc/sysrq-trigger
# 立即关闭计算机
echo "o" > /proc/sysrq-trigger
# 导出内存分配的信息 (可以用/var/log/message 查看)
echo "m" > /proc/sysrq-trigger
# 导出当前CPU寄存器信息和标志位的信息
echo "p" > /proc/sysrq-trigger
# 导出线程状态信息
echo "t" > /proc/sysrq-trigger
# 故意让系统崩溃(用于测试,比如测试RHCS的fence设备)
echo "c" > /proc/sysrq-trigger
# 立即重新挂载所有的文件系统
echo "s" > /proc/sysrq-trigger
# 立即重新挂载所有的文件系统为只读
echo "u" > /proc/sysrq-trigger
Documentation for sysrq.c Based on kernel version 2.6.38. Page generated on 2011-03-22 22:20 EST. Linux Magic System Request Key Hacks Documentation for sysrq.c * What is the magic SysRq key? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ It is a 'magical' key combo you can hit which the kernel will respond to regardless of whatever else it is doing, unless it is completely locked up. * How do I enable the magic SysRq key? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You need to say "yes" to 'Magic SysRq key (CONFIG_MAGIC_SYSRQ)' when configuring the kernel. When running a kernel with SysRq compiled in, /proc/sys/kernel/sysrq controls the functions allowed to be invoked via the SysRq key. By default the file contains 1 which means that every possible SysRq request is allowed (in older versions SysRq was disabled by default, and you were required to specifically enable it at run-time but this is not the case any more). Here is the list of possible values in /proc/sys/kernel/sysrq: 0 - disable sysrq completely 1 - enable all functions of sysrq >1 - bitmask of allowed sysrq functions (see below for detailed function description): 2 - enable control of console logging level 4 - enable control of keyboard (SAK, unraw) 8 - enable debugging dumps of processes etc. 16 - enable sync command 32 - enable remount read-only 64 - enable signalling of processes (term, kill, oom-kill) 128 - allow reboot/poweroff 256 - allow nicing of all RT tasks You can set the value in the file by the following command: echo "number" >/proc/sys/kernel/sysrq Note that the value of /proc/sys/kernel/sysrq influences only the invocation via a keyboard. Invocation of any operation via /proc/sysrq-trigger is always allowed (by a user with admin privileges). * How do I use the magic SysRq key? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ On x86 - You press the key combo 'ALT-SysRq-<command key>'. Note - Some keyboards may not have a key labeled 'SysRq'. The 'SysRq' key is also known as the 'Print Screen' key. Also some keyboards cannot handle so many keys being pressed at the same time, so you might have better luck with "press Alt", "press SysRq", "release SysRq", "press <command key>", release everything. On SPARC - You press 'ALT-STOP-<command key>', I believe. On the serial console (PC style standard serial ports only) - You send a BREAK, then within 5 seconds a command key. Sending BREAK twice is interpreted as a normal BREAK. On PowerPC - Press 'ALT - Print Screen (or F13) - <command key>, Print Screen (or F13) - <command key> may suffice. On other - If you know of the key combos for other architectures, please let me know so I can add them to this section. On all - write a character to /proc/sysrq-trigger. e.g.: echo t > /proc/sysrq-trigger * What are the 'command' keys? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'b' - Will immediately reboot the system without syncing or unmounting your disks. 'c' - Will perform a system crash by a NULL pointer dereference. A crashdump will be taken if configured. 'd' - Shows all locks that are held. 'e' - Send a SIGTERM to all processes, except for init. 'f' - Will call oom_kill to kill a memory hog process. 'g' - Used by kgdb on ppc and sh platforms. 'h' - Will display help (actually any other key than those listed here will display help. but 'h' is easy to remember :-) 'i' - Send a SIGKILL to all processes, except for init. 'j' - Forcibly "Just thaw it" - filesystems frozen by the FIFREEZE ioctl. 'k' - Secure Access Key (SAK) Kills all programs on the current virtual console. NOTE: See important comments below in SAK section. 'l' - Shows a stack backtrace for all active CPUs. 'm' - Will dump current memory info to your console. 'n' - Used to make RT tasks nice-able 'o' - Will shut your system off (if configured and supported). 'p' - Will dump the current registers and flags to your console. 'q' - Will dump per CPU lists of all armed hrtimers (but NOT regular timer_list timers) and detailed information about all clockevent devices. 'r' - Turns off keyboard raw mode and sets it to XLATE. 's' - Will attempt to sync all mounted filesystems. 't' - Will dump a list of current tasks and their information to your console. 'u' - Will attempt to remount all mounted filesystems read-only. 'v' - Dumps Voyager SMP processor info to your console. 'w' - Dumps tasks that are in uninterruptable (blocked) state. 'x' - Used by xmon interface on ppc/powerpc platforms. 'z' - Dump the ftrace buffer '0'-'9' - Sets the console log level, controlling which kernel messages will be printed to your console. ('0', for example would make it so that only emergency messages like PANICs or OOPSes would make it to your console.)