`

我使用过的Linux命令之screen - 虚拟终端屏幕管理器

阅读更多

我使用过的Linux命令之screen - 虚拟终端屏幕管理器

本文链接:http://codingstandards.iteye.com/blog/781839    (转载请注明出处)

用途说明

      经常在维护Linux服务器的时候发现,由于网络连接突然断开,正在执行的程序会非正常结束。好不容易运行了很长时间的程序,就这么挂了,痛心啊!当然你也可以改用后台运行,但有时候就是想看着它执行,为啥?怕出问题啊,而且有些程序不适合后台执行。这一切,在你知道有个screen命令之后,就会Over了!!!

      screen命令就是一个这样的虚拟终端程序,你运行它之后,它的表现形式就是一个shell,你可以在它上面运行自己想运行的命令,尤其是那些耗时比较长的命令,如果不幸网络掉线,也没有关系,你可以重新连上去,发现程序还在那儿欢快的跑着呢,真是爽极了~!而且更强大的是,它还可以多人协作,适合于几个人共同解决问题,也可用于Linux教学。

      但不是每个Linux系统在安装完之后有screen这个命令,你有可能很失望的发现,执行结果是这样的。

[root@qzt196 ~]# screen
-bash: screen: command not found

      不过不用着急,找一下安装光盘,把它挂上去,然后去找screen-*.rpm,安装一下就可以了,如下所示:

[root@qzt196 ~]# mount /dev/cdrom
[root@qzt196 ~]# cd /mnt/cdrom
[root@qzt196 cdrom]# find . -name "screen-*rpm"
./Server/screen-4.0.3-1.el5.i386.rpm
[root@qzt196 cdrom]# rpm -ivh ./enterprise/Server/screen-4.0.3-1.el5.i386.rpm
warning: ./Server/screen-4.0.3-1.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing...                ########################################### [100%]
   1:screen                 ########################################### [100%]
[root@qzt196 cdrom]# rpm -q screen
screen-4.0.3-1.el5

[root@qzt196 cdrom]# cd

[root@qzt196 ~]# screen

当前屏幕被清除,在顶上显示。这下就可以轻松使用screen命令了。

[root@qzt196 ~]#

常用参数

-x 连接到某个已经存在的screen上。如果没有,报错;只有一个,直接连接上;有多个,显示列表。

-S <sessionname> 创建名为sessionname的screen。

-x <sessionname> 连接到名为sessionname的screen。

Ctrl+a d 暂时离开screen。

exit 退出screen。

使用示例

示例一

[root@qzt196 ~]# screen
进入screen,屏幕清除了。
[root@qzt196 ~]# top
top - 02:42:24 up 11:39,  2 users,  load average: 0.00, 0.00, 0.00
Tasks: 211 total,   1 running, 210 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  14420992k total ,    713096k used, 13707896k free,   135132k buffers  <=== 今天才发现这台机器有多余14G的内存,强大。
Swap:  2031608k total,        0k used,  2031608k free,   267244k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                              
29446 root      15   0  9008 2736 2188 S    0  0.0   0:00.23 sshd                                                                  
29528 root      15   0  6116 1160  696 S    0  0.0   0:00.04 screen                                                                
29549 root      15   0  2316 1068  784 R    0  0.0   0:00.47 top                                                                   
    1 root      15   0  2056  636  544 S    0  0.0   0:00.67 init                                                                  
    2 root      RT   0     0    0    0 S    0  0.0   0:00.04 migration/0                                                           
    3 root      34  19     0    0    0 S    0  0.0   0:00.02 ksoftirqd/0                                                           
    4 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/0                                                            
    5 root      RT   0     0    0    0 S    0  0.0   0:00.07 migration/1                                                           
    6 root      34  19     0    0    0 S    0  0.0   0:00.00 ksoftirqd/1                                                           
    7 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/1                                                            
    8 root      RT   0     0    0    0 S    0  0.0   0:00.04 migration/2                                                           
    9 root      35  19     0    0    0 S    0  0.0   0:00.00 ksoftirqd/2                                                           
   10 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/2                                                            
省略更多输出       

Ctrl+a d      <=== 注:此处是先按Ctrl+a,然后按d。                                               
[detached]
[root@qzt196 ~]#

 

 

换一个终端登录。

Last login: Sun May 16 02:29:20 2010 from 222.70.144.138
[root@qzt196 ~]# screen -x    <=== 你会发现运行之后会出现top运行的全屏界面。

top - 02:42:39 up 11:39,  3 users,  load average: 0.00, 0.00, 0.00
Tasks: 210 total,   1 running, 209 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  14420992k total,   712972k used, 13708020k free,   135156k buffers
Swap:  2031608k total,        0k used,  2031608k free,   267244k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                              
29549 root      15   0  2316 1068  784 R    0  0.0   0:00.52 top                                                                   
    1 root      15   0  2056  636  544 S    0  0.0   0:00.67 init                                                                  
    2 root      RT   0     0    0    0 S    0  0.0   0:00.04 migration/0                                                           
    3 root      34  19     0    0    0 S    0  0.0   0:00.02 ksoftirqd/0                                                           
    4 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/0                                                            
    5 root      RT   0     0    0    0 S    0  0.0   0:00.07 migration/1                                                           
    6 root      34  19     0    0    0 S    0  0.0   0:00.00 ksoftirqd/1                                                           
    7 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/1                                                            
    8 root      RT   0     0    0    0 S    0  0.0   0:00.04 migration/2                                                           
    9 root      35  19     0    0    0 S    0  0.0   0:00.00 ksoftirqd/2                                                           
   10 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/2                                                            
省略更多输出       

Ctrl+a d                                                      
[detached]
[root@qzt196 ~]#

示例二 网络断线测试

先screen,然后运行top,然后强行把远程连接断开,注意此处不需要先按Ctrl+a d,直接叉掉窗口就行了。

然后重新登录,看看top还在不在。

Last login: Sun May 16 02:40:12 2010 from 222.70.144.138
[root@qzt196 ~]# screen -x
top - 02:52:58 up 11:49,  2 users,  load average: 0.09, 0.02, 0.01
Tasks: 209 total,   1 running, 208 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:  14420992k total,   714212k used, 13706780k free,   136008k buffers
Swap:  2031608k total,        0k used,  2031608k free,   267244k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                              
29549 root      15   0  2316 1068  784 R    0  0.0   0:02.66 top                                                                   
    1 root      15   0  2056  636  544 S    0  0.0   0:00.67 init                                                                  
    2 root      RT   0     0    0    0 S    0  0.0   0:00.04 migration/0                                                           
    3 root      34  19     0    0    0 S    0  0.0   0:00.02 ksoftirqd/0                                                           
    4 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/0                                                            
    5 root      RT   0     0    0    0 S    0  0.0   0:00.07 migration/1                                                           
    6 root      34  19     0    0    0 S    0  0.0   0:00.00 ksoftirqd/1                                                           
    7 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/1                                                            
    8 root      RT   0     0    0    0 S    0  0.0   0:00.04 migration/2                                                           
    9 root      35  19     0    0    0 S    0  0.0   0:00.00 ksoftirqd/2                                                           
   10 root      RT   0     0    0    0 S    0  0.0   0:00.00 watchdog/2                                                            
省略更多输出       

果然还在。

q
[root@qzt196 ~]# exit
exit

[screen is terminating]

screen虚拟终端彻底退出了。
[root@qzt196 ~]# screen -x
There is no screen to be attached.
[root@qzt196 ~]#

 

示例三 多人同时编辑文件

从一个远程连接上执行screen命令,蓝色字体为本终端输入的内容。

Last login: Sun May 16 02:52:51 2010 from 222.70.144.138
[root@qzt196 ~]# screen vi 1.txt
Hello
World
~                                                                                                                                  
在完成后面那屏幕的输入后,按 :wq 退出vi。
"1.txt" [New] 2L, 12C written

[screen is terminating]
[root@qzt196 ~]#

 

从另一终端上登录,连接到前面的screen上。发现vi界面,而且可以编辑。

Last login: Sun May 16 02:59:58 2010 from 222.70.144.138
[root@qzt196 ~]# screen -x
Hello
World 切换到前面那屏幕,你会发现那个屏幕上也会出现折行。
~                                                                                                                                  
"1.txt" [New] 2L, 12C written

[screen is terminating]
[root@qzt196 ~]#

 

示例四 指定名字

创建一个名为helpme的screen。

[root@qzt196 ~]# screen -S helpme
[root@qzt196 ~]# vi 1.txt
Hello
World
~                                                                                                                                  
"1.txt" 2L, 12C

 

 

另外登录一个终端,连接到名为helpme的screen上。

[root@qzt196 ~]# screen -x helpme
Hello
World
~                                                                                                                                  
"1.txt" 2L, 12C

问题思考

1. 为什么关掉窗口/断开连接会使得正在运行的程序死掉?

2. screen有哪些快捷键?

3. 怎样显示当前有哪些screen?

相关资料

【1】IBM linux 技巧:使用 screen 管理你的远程会话

【2】ZDNet 服务器频道 使用Screen创建虚拟终端避免Linux远程断线

 

返回 我使用过的Linux命令系列总目录

 

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics