LMLPHP后院

Linux下screen命令使用教程技术

maybe yes 发表于 2015-07-16 00:30

Linux 下的 screen 命令能够方便的管理桌面会话,每个会话可以包含多个屏幕。使用 screen 可以快捷的保存工作环境,比如有的任务需要执行很长时间,或者网络不稳定的情况下保存桌面环境,下次使用 SSH 登录的时候可以直接进入某个会话,直接提升工作效率。

一开始,想到使用 screen,主要是有些进程需要执行很长时间如果连接突然断开,或者网络不稳定的情况下会话退出了,进程就会被终止。使用 screen 可以很好的解决这个问题,使得进程可以常驻后台运行。本人使用的是 Ubuntu 系统,相信其他系统也差不多一样。在 Ubuntu安装非常方便,直接 $ apt-get install screen 就可以搞定。

网上的一些关于 screen 的教程,都不是很清晰,特别是对这个不懂的人,估计看了还是不会玩,下面以通俗的语言来玩玩 screen 命令

启用 screen,同时开启一个 screen 会话


$ screen

// 执行上面的命令后出现如下全屏界面

Screen version 4.00.03jw4 (FAU) 2-May-06

Copyright (c) 1993-2002 Juergen Weigert, Michael Schroeder
Copyright (c) 1987 Oliver Laumann

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either
version 2, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program (see the file COPYING); if not, write to the Free Software Foundation, Inc., 51
Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to screen@uni-erlangen.de

To use byobu (formerly screen-profiles), exit screen and run 'byobu'.



                                [Press Space or Return to end.]

接着按下回车或者空白就可以,这样系统就会接受 screen 的一些快捷键命令,如按下 ctrl + a 组合键后,然后按下 n ,屏幕左下角会提示 No other window.。

查看有哪些 screen 会话,使用 screen -ls,如下示例。

root@may:~# screen -ls
There are screens on:
        15985.pts-2.may (07/15/2015 12:32:44 PM)        (Attached)
        6293.pts-6.may  (07/14/2015 06:13:38 AM)        (Detached)
        11402.pts-0.may (06/23/2015 05:46:22 AM)        (Detached)
3 Sockets in /var/run/screen/S-root.

root@may:~#

上面例子中最上面的一个会话,就是刚刚打开的。创建一个指定名称的会话,可以使用命令 screen -S name。

screen 的状态有两种,Attached 和 Detached,分别表示前台运行和后台运行。使用 screen -r 会话名 可以将 Detached 状态的屏幕会话状态修改为前台运行,并同时切换到这个会话中。如果会话已经是 Attached 状态下,使用命令 screen -r 会话名 是不能切换到那个会话下的,会提示类似于 There is a screen on:15985.pts-2.may (07/15/2015 12:32:44 PM) (Attached) There is no screen to be resumed matching 15985.pts-2.may. 。如果需要切换到状态为 Attached 的会话下,需要先执行 screen -d 会话名 将会话状态修改为 Detached。将当前会话转为后台运行可以使用快捷键 ctrl+a, d 。如下示例:


root@may:~# screen -r 6293.pts-6.may

...


root@may:/srv/www/webchat.lmlphp.com# screen -ls
There are screens on:
        15985.pts-2.may (07/15/2015 12:32:44 PM)        (Attached)
        6293.pts-6.may  (07/14/2015 06:13:38 AM)        (Attached)
        11402.pts-0.may (06/23/2015 05:46:22 AM)        (Detached)
3 Sockets in /var/run/screen/S-root.

root@may:/srv/www/webchat.lmlphp.com# screen -r 15985.pts-2.may
There is a screen on:
        15985.pts-2.may (07/15/2015 12:32:44 PM)        (Attached)
There is no screen to be resumed matching 15985.pts-2.may.
root@may:/srv/www/webchat.lmlphp.com#

screen 的控制快捷键是 ctrl+a,所有的命令都是在这个控制信号之后,所以在 screen 模式下,ctrl+a 组合是不会将光标回到命令行的最前面的。相关的快捷键如下:
ctrl-a c:创建一个新的 Shell 窗口
ctrl-a ctrl-a:在 Shell 间切换,或者按住 ctrl 连按两次 a
ctrl-a w 显示所有的 Shell 窗口
ctrl-a n 切换到下一个 Shell
ctrl-a p 切换到上一个 Shell
ctrl-a 0…9 同样是切换各个 Shell
ctrl-a d 将会话转为后台运行

一般情况下,需要断开连接时,最好将所有的 screen 会话状态全部改为 Detached,这样更安全,更稳定,Attached 状态下容易丢失窗口。如果需要删除某个屏幕会话,可以切换到那个屏幕下,使用 exit 命令,或者在会话下使用 ctrl+a k 会话名,系统会提示是否全部关闭所有窗口。

相关文章
2024-04-26 05:25:43 1714080343 0.040437