ZHANG BIAO . ORG

^


linux screen 命令详解

• LIUNX • 阅读: 68591

linux screen 命令详解
一、背景

系统管理员经常需要SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,比如系统备份、ftp 传输等等。通常情况下我们都是为每一个这样的任务开一个远程终端窗口,因为它们执行的时间太长了。必须等待它们执行完毕,在此期间不能关掉窗口或者断开连接,否则这个任务就会被杀掉,一切半途而废了。

二、简介

GNU Screen是一款由GNU计划开发的用于命令行终端切换的自由软件。用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换。

GNU Screen可以看作是窗口管理器的命令行界面版本。它提供了统一的管理多个会话的界面和相应的功能。

会话恢复
只要Screen本身没有终止,在其内部运行的会话都可以恢复。这一点对于远程登录的用户特别有用——即使网络连接中断,用户也不会失去对已经打开的命令行会话的控制。只要再次登录到主机上执行screen -r就可以恢复会话的运行。同样在暂时离开的时候,也可以执行分离命令detach,在保证里面的程序正常运行的情况下让Screen挂起(切换到后台)。这一点和图形界面下的VNC很相似。
多窗口
在Screen环境下,所有的会话都独立的运行,并拥有各自的编号、输入、输出和窗口缓存。用户可以通过快捷键在不同的窗口下切换,并可以自由的重定向各个窗口的输入和输出。Screen实现了基本的文本操作,如复制粘贴等;还提供了类似滚动条的功能,可以查看窗口状况的历史记录。窗口还可以被分区和命名,还可以监视后台窗口的活动。
会话共享
Screen可以让一个或多个用户从不同终端多次登录一个会话,并共享会话的所有特性(比如可以看到完全相同的输出)。它同时提供了窗口访问权限的机制,可以对窗口进行密码保护。
GNU's Screen 官方站点:http://www.gnu.org/software/screen/

三、语法

screen -AmRvx -ls -wipe-h <行数>-s

参数说明

-A  将所有的视窗都调整为目前终端机的大小。
-d <作业名称>  将指定的screen作业离线。
-h <行数>  指定视窗的缓冲区行数。
-m  即使目前已在作业中的screen作业,仍强制建立新的screen作业。
-r <作业名称>  恢复离线的screen作业。
-R  先试图恢复离线的作业。若找不到离线的作业,即建立新的screen作业。
-s  指定建立新视窗时,所要执行的shell。
-S <作业名称>  指定screen作业的名称。
-v  显示版本信息。
-x  恢复之前离线的screen作业。
-ls或--list  显示目前所有的screen作业。
-wipe  检查目前所有的screen作业,并删除已经无法使用的screen作业。

四、常用screen参数

screen -S yourname -> 新建一个叫yourname的session
screen -ls -> 列出当前所有的session
screen -r yourname -> 回到yourname这个session
screen -d yourname -> 远程detach某个session
screen -d -r yourname -> 结束当前session并回到yourname这个session

在每个screen session 下,所有命令都以 ctrl+a(C-a) 开始。
C-a ? -> 显示所有键绑定信息
C-a c -> 创建一个新的运行shell的窗口并切换到该窗口
C-a n -> Next,切换到下一个 window
C-a p -> Previous,切换到前一个 window
C-a 0..9 -> 切换到第 0..9 个 window
Ctrl+a [Space] -> 由视窗0循序切换到视窗9
C-a C-a -> 在两个最近使用的 window 间切换
C-a x -> 锁住当前的 window,需用用户密码解锁
C-a d -> detach,暂时离开当前session,将目前的 screen session (可能含有多个 windows) 丢到后台执行,并会回到还没进 screen 时的状态,此时在 screen session 里,每个 window 内运行的 process (无论是前台/后台)都在继续执行,即使 logout 也不影响。
C-a z -> 把当前session放到后台执行,用 shell 的 fg 命令则可回去。
C-a w -> 显示所有窗口列表
C-a t -> Time,显示当前时间,和系统的 load
C-a k -> kill window,强行关闭当前的 window
C-a [ -> 进入 copy mode,在 copy mode 下可以回滚、搜索、复制就像用使用 vi 一样

C-b Backward,PageUp 
C-f Forward,PageDown 
H(大写) High,将光标移至左上角 
L Low,将光标移至左下角 
0 移到行首 
$ 行末 
w forward one word,以字为单位往前移 
b backward one word,以字为单位往后移 
Space 第一次按为标记区起点,第二次按为终点 
Esc 结束 copy mode 

C-a ] -> Paste,把刚刚在 copy mode 选定的内容贴上

文章标签: liunx
点此生成 >> 文章海报


已经是第一篇文章了~
下一篇 : 2021年双十二,博客搭建完成
留言




已有 28310 条留言
  1. majesticgrov
    2026-05-22 15:05:00
    回复

    Just want to flag that this was useful and not bury the appreciation in caveats, and a look at majesticgrovers earned the same direct praise, recognising good work without hedging it with criticism is something I try to practice because over qualified compliments tend to read as backhanded and miss the point sometimes.

  2. simplegiftma
    2026-05-22 15:05:08
    回复

    Quality writing that respects the reader's intelligence without overloading them, and a quick look at simplegiftmarket reflected that approach, a balanced thoughtful site that earns trust by being consistent rather than by shouting about how trustworthy it is which is the usual approach online sadly across most content categories.

  3. 2026-05-22 15:05:19
    回复

    Vague feelings of recognition kept surfacing as I read because the writing names things I have been thinking, and a look at cozycabincreations produced more of those recognition moments, content that gives shape to private intuitions is content that makes me feel less alone in my own thinking and this site has that effect.

  4. 2026-05-22 15:05:31
    回复

    Thank you for the genuine effort here, it shows in every paragraph and not just the headline, and after my visit to happychoicecorner I was sure this site cares about getting things right rather than chasing clicks, which is the main reason I will come back later this week to read more.

  5. shoptheday
    2026-05-22 16:05:20
    回复

    Really appreciate this kind of writing, no shouting and no clickbait headlines just steady useful content, and a quick look at shoptheday kept that going, definitely a site I will be returning to whenever I need a sensible take on similar topics in the days ahead and also during slower work weeks.

  6. finduniqueof
    2026-05-22 16:05:51
    回复

    Yesterday I was complaining about the state of online writing and today this site has temporarily fixed that complaint, and a look at finduniqueoffers extended that mood reversal, the short term mood improvement that comes from finding good content is real and this site has produced that improvement for me at a useful moment.

  7. 2026-05-22 16:05:09
    回复

    Liked that the post acknowledged complications rather than pretending they did not exist, and a stop at softstoneoffering continued that honest framing, sites that handle complexity with care rather than papering it over with simplifying claims are doing real intellectual work and this one is clearly in that category based on what I have read.

  8. createpositi
    2026-05-22 16:05:51
    回复

    Thanks for taking the time to write this, it is clear that some thought went into how each point would land, and after I went through createpositivechange I had a better grip on the topic, real value without the usual marketing noise people have to put up with online when searching for answers.

  9. 2026-05-22 16:05:56
    回复

    Thanks for the moderate length, neither so short it skips substance nor so long it bloats, and a stop at urbanseedcenter hit the same balance, the right length is one of the hardest things to calibrate in blog writing and I appreciate when a team has clearly thought about it rather than defaulting.

  10. yourjourneyc
    2026-05-22 16:05:48
    回复

    Useful reading material, the kind I can hand off to someone newer to the topic without worrying about confusing them, and a quick look at yourjourneycontinues confirmed the same beginner friendly tone runs throughout the site which is great for sharing with people just starting their learning journey on this particular topic.

  11. 2026-05-22 16:05:02
    回复

    Decided to read more before commenting and the more I read the more I wanted to say something, and a stop at trendysaleoutlet pushed that impulse further, when content provokes the urge to participate rather than just consume it is doing something quite specific and worth recognising clearly when it happens during reading.

  12. 2026-05-22 16:05:08
    回复

    The post made the topic feel approachable without making it feel trivial, that is a fine balance, and a stop at sunwaveessentials maintained the same balance, finding the middle ground between welcoming and serious is genuinely difficult and the writers here have clearly figured out how to consistently hit it well across many different posts.

  13. 2026-05-22 16:05:14
    回复

    Speaking from the perspective of having read widely on the topic this site offers something distinct, and a look at timelessstyleplace reinforced that distinctness, the rare site that contributes something genuinely original to a saturated topic is the rare site worth following carefully and this one has demonstrated that original contribution capability today.

  14. simplevalueh
    2026-05-22 16:05:22
    回复

    Now appreciating the way the post avoided the temptation to be longer than necessary, and a look at simplevaluehub continued that lean approach, content with the discipline to stop when finished rather than padding for length is content that respects both itself and its readers and this site has that disciplined editorial culture clearly throughout.

  15. yourdailyfin
    2026-05-22 16:05:44
    回复

    A piece that respected the reader by not over explaining the obvious, and a look at yourdailyfinds continued that calibrated approach, finding the right level of explanation is one of the harder editorial calls and this site has clearly thought carefully about what readers will already know versus what they need help with consistently.

  16. discoverneww
    2026-05-22 16:05:56
    回复

    Honestly the simplicity is what makes this work, the topic is not buried under filler words or overly complex examples, and a quick look at discovernewworlds showed the same sensible style, I left with what I came for and no headache from over reading which is a real win these days.

  17. 2026-05-22 16:05:10
    回复

    If quality blog writing is dying as people sometimes claim then this site is one piece of evidence that it has not died yet, and a look at inspireyourjourney extended that evidence, the broader cultural question about online writing has empirical answers in specific sites and this one is contributing to a more optimistic answer overall.

  18. 2026-05-22 16:05:57
    回复

    Now adjusting my mental model of how the topic fits into the broader landscape, and a look at growwithdetermination extended that adjustment, content that affects my structural understanding rather than just my factual knowledge is content with deeper impact and this site is providing those structural updates at a meaningful rate consistently across topics.

  19. bestpickshub
    2026-05-22 16:05:40
    回复

    Took a screenshot of one section to come back to later, and a stop at bestpickshub prompted another saved tab, the urge to capture and revisit specific pieces of content is something I rarely feel but when I do it tells me the work is worth more than the average passing read for sure.

  20. brightleafem
    2026-05-22 16:05:57
    回复

    Now feeling the rare pleasure of trusting a source completely on first encounter, and a look at brightleafemporium extended that initial trust into something more durable, the calibration of trust to evidence is something I do informally and this site has earned high trust through the cumulative weight of multiple consistently good posts already.