有时通过SecureCRT或者Putty远程ssh到主机上执行某个进程,因长时间没有交互导致ssh断链,此时该进程由init进程收留。该进程的输出也就无法获得了。
这种情况下,可以利用gdb重新获得该进程的标准输出,方法如下:
1:获得程序的进程号(PID),以某个python进程为例:
# ps –ef|grep python …… root 22167 1 0 18:15 ? 00:00:00 python test.py ……
2:使用gdb调试这个进程:
# gdb -p 22167 GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1) Copyright (C) 2010 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-redhat-linux-gnu". For bug reporting instructions, please see: <http://www.gnu.org/software/gdb/bugs/>. Attaching to process 22167 Reading symbols from /usr/bin/python...(no debugging symbols found)...done. Reading symbols from /usr/lib64/libpython2.6.so.1.0...(no debugging symbols found)...done. Loaded symbols for /usr/lib64/libpython2.6.so.1.0 Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done. [Thread debugging using libthread_db enabled] Loaded symbols for /lib64/libpthread.so.0 Reading symbols from /lib64/libdl.so.2...(no debugging symbols found)...done. Loaded symbols for /lib64/libdl.so.2 Reading symbols from /lib64/libutil.so.1...(no debugging symbols found)...done. Loaded symbols for /lib64/libutil.so.1 Reading symbols from /lib64/libm.so.6...(no debugging symbols found)...done. Loaded symbols for /lib64/libm.so.6 Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done. Loaded symbols for /lib64/libc.so.6 Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done. Loaded symbols for /lib64/ld-linux-x86-64.so.2 Reading symbols from /usr/lib64/python2.6/lib-dynload/timemodule.so...(no debugging symbols found)...done. Loaded symbols for /usr/lib64/python2.6/lib-dynload/timemodule.so Reading symbols from /usr/lib64/python2.6/lib-dynload/selectmodule.so...(no debugging symbols found)...done. Loaded symbols for /usr/lib64/python2.6/lib-dynload/selectmodule.so Reading symbols from /usr/lib64/python2.6/lib-dynload/fcntlmodule.so...(no debugging symbols found)...done. Loaded symbols for /usr/lib64/python2.6/lib-dynload/fcntlmodule.so Reading symbols from /usr/lib64/python2.6/lib-dynload/_struct.so...(no debugging symbols found)...done. Loaded symbols for /usr/lib64/python2.6/lib-dynload/_struct.so Reading symbols from /usr/lib64/python2.6/lib-dynload/binascii.so...(no debugging symbols found)...done. Loaded symbols for /usr/lib64/python2.6/lib-dynload/binascii.so Reading symbols from /usr/lib64/python2.6/lib-dynload/cStringIO.so...(no debugging symbols found)...done. Loaded symbols for /usr/lib64/python2.6/lib-dynload/cStringIO.so Reading symbols from /usr/lib64/python2.6/lib-dynload/_functoolsmodule.so...(no debugging symbols found)...done. Loaded symbols for /usr/lib64/python2.6/lib-dynload/_functoolsmodule.so Reading symbols from /usr/lib64/python2.6/lib-dynload/_collectionsmodule.so...(no debugging symbols found)...done. Loaded symbols for /usr/lib64/python2.6/lib-dynload/_collectionsmodule.so Reading symbols from /usr/lib64/python2.6/lib-dynload/operator.so...(no debugging symbols found)...done. Loaded symbols for /usr/lib64/python2.6/lib-dynload/operator.so Reading symbols from /usr/lib64/python2.6/lib-dynload/itertoolsmodule.so...(no debugging symbols found)...done. Loaded symbols for /usr/lib64/python2.6/lib-dynload/itertoolsmodule.so 0x0000003f7e8e15e3 in select () from /lib64/libc.so.6 Missing separate debuginfos, use: debuginfo-install python-2.6.6-51.el6.x86_64
或者可以先运行 gdb ,然后用 attach 命令,如下:
$gdb $atta 22167 ......
3:GDB 提供的 call 命令允许调试者在当前函数调用栈的栈顶调用函数,犹如在被调试的程序中执行的一般。可以通过close系统调用关闭标准输出(STDOUT)或者标准错误(STDERR):
(gdb) call close(1) $1 = 0
4:通过open系统调用打开一个文件,并将其文件描述符通过dup2系统调用复制给标准输出:
(gdb) call dup2(open(”/tmp/log”, 2), 1) $2 = 1
5:如果希望将标准输出定位到当前终端上,可以执行下面的命令:
(gdb) shell tty /dev/pts/0 (gdb) call dup2(open ("/dev/pts/0",2) , 1) $2 = 1
6:退出gdb。
(gdb) quit A debugging session is active. Inferior 1 [process 22167] will be detached. Quit anyway? (y or n) y Detaching from program: /usr/bin/python, process 22167
现在就可以通过/tmp/log或者当前终端重新获得该进程的标准输出了。
参考:
http://www.ibm.com/developerworks/cn/linux/l-cn-redirect/
http://blog.csdn.net/lida2003/article/details/7899777
http://www.michael--martinez.com/computers/controlterm.html