<section id="nice" data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="color: black; padding: 0 10px; word-spacing: 0px; word-break: break-word; word-wrap: break-word; text-align: left; font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; font-size: 14px; letter-spacing: 0.05em; line-height: 2.0;"><p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">screen 是一款由 GNU 开发的命令行终端工具,它提供了从多个终端窗口连接到同一个 shell 会话(会话共享)。当网络中断,或终端窗口意外关闭是,中 screen 中运行的程序任然可以运行(系统自带的终端窗口,当窗口意外关闭时,在该终端窗口中运行的程序也会终止。)。</p>
<figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://www.icode9.com/i/ll/?i=20210125214049892.png" alt style="display: block; margin: 0 auto; max-width: 100%; box-shadow: 0px 0px 10px #333; border-radius: 5px 5px 5px 5px;"></figure>
<h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">安装 screen 工具</span><span class="suffix" style="display: none;"></span></h4>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">it@serverc:~$ sudo apt install screen -y<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">* 在 CentOS 中,你需要先安装 epel-release 才可以安装 screen( 在 CentOS 中安装软件使用 yum 或者 dnf )</p>
<h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">会话共享</span><span class="suffix" style="display: none;"></span></h4>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">在 A 主机上创建一个名为 it 的 screen 会话</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">it@serverc:~$ screen -S it<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">在 B 主机上,通过 ssh 连接到 A 主机,并查通过 <strong style="font-weight: bold; color: rgb(71, 193, 168);">-ls</strong> 选项看当前已经存在的 screen 会话</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">it@workstation:~/ansible$ ssh it@10.10.10.108<br>it@10.10.10.108<span class="hljs-string" style="color: #98c379; line-height: 26px;">'s password: <br>Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-60-generic x86_64)<br>... ... ... ...<br>... ... ... ...<br>Last login: Thu Jan 14 13:57:32 2021<br>it@serverc:~$ screen -ls<br>There is a screen on:<br> 2107.it (01/14/21 07:30:27) (Attached)<br>1 Socket in /run/screen/S-it.<br></span></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">然后加入到该会话</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">it@serverc:~$ screen -x it<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">这样就可以两边共享一个 screen 会话,不管谁运行什么命令,另一个都可以看到;</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">当你需要退出会话时,你可以运行 <strong style="font-weight: bold; color: rgb(71, 193, 168);">exit</strong> 来退出会话(当运行 exit 时,两边都会退出 screen 会话。),也可以通过 <kbd>Ctrl</kbd> + <kbd>A</kbd>, <kbd>D</kbd> (Ctrl + A 然后按 D,退出单个会话)。</p>
<h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">当网络中断或窗口意外关闭后,任务继续运行</span><span class="suffix" style="display: none;"></span></h4>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">在 Linux 打开两个终端窗口,我们分部称他们为 A 窗口和 B 窗口,在 A 窗口中运行 ping</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">it@workstation:~$ ping 10.10.10.1<br>PING 10.10.10.1 (10.10.10.1) 56(84) bytes of data.<br>64 bytes from 10.10.10.1: icmp_seq=1 ttl=64 time=1.21 ms<br>64 bytes from 10.10.10.1: icmp_seq=2 ttl=64 time=1.22 ms<br>64 bytes from 10.10.10.1: icmp_seq=3 ttl=64 time=1.19 ms<br>... ... ... ...<br>... ... ... ...<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">然后在 B 窗口,运行 <strong style="font-weight: bold; color: rgb(71, 193, 168);">ps aux</strong> 获取系统进程,然后通过 grep 进行筛选</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">it@workstation:~$ ps aux | grep ping<br>it 2336 0.0 0.2 317144 8740 ? Ssl 2020 0:17 /usr/libexec/gsd-housekeeping<br>it 153585 0.0 0.0 12764 872 pts/3 S+ 14:53 0:00 ping 10.10.10.1<br>it 153646 0.0 0.0 12108 736 pts/2 S+ 14:56 0:00 grep --color=auto ping<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">我们看到 ping 正在运行</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">当关掉 A 窗口后,我们再到 B 窗口(此时就只有一个窗口了)运行之前的 <strong style="font-weight: bold; color: rgb(71, 193, 168);">ps aux</strong> 命令</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">it@workstation:~$ ps aux | grep ping<br>it 2336 0.0 0.2 317144 8740 ? Ssl 2020 0:17 /usr/libexec/gsd-housekeeping<br>it 153651 0.0 0.0 12108 2964 pts/2 S+ 14:57 0:00 grep --color=auto ping<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">此时 ping 命令已经结束了</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">重新打开一个新的终端窗口,作为 A 窗口,运行 <strong style="font-weight: bold; color: rgb(71, 193, 168);">screen</strong></p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">it@serverc:~$ screen -S <span class="hljs-built_in" style="color: #e6c07b; line-height: 26px;">test</span><br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">然后在 screen 窗口中运行 ping 命令</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">it@workstation:~$ ping 10.10.10.1<br>PING 10.10.10.1 (10.10.10.1) 56(84) bytes of data.<br>64 bytes from 10.10.10.1: icmp_seq=1 ttl=64 time=1.21 ms<br>64 bytes from 10.10.10.1: icmp_seq=2 ttl=64 time=1.15 ms<br>64 bytes from 10.10.10.1: icmp_seq=3 ttl=64 time=1.21 ms<br>... ... ... ...<br>... ... ... ...<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">然后关闭 A 窗口,回到 B 窗口,运行 <strong style="font-weight: bold; color: rgb(71, 193, 168);">ps aux</strong> 命令;</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">it@workstation:~$ ps aux | grep ping<br>it 2336 0.0 0.2 317144 8684 ? Ssl 2020 0:17 /usr/libexec/gsd-housekeeping<br>it 154964 0.0 0.0 12768 940 pts/3 S+ 15:02 0:00 ping 10.10.10.1<br>it 154986 0.0 0.0 12108 736 pts/4 S+ 15:05 0:00 grep --color=auto ping<br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">我们发现 ping 命令还在继续运行</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">当我们想回到之前的 ping 会话,可以通过 <strong style="font-weight: bold; color: rgb(71, 193, 168);">screen -r</strong> 恢复会话</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">it@workstation:~$ screen -r <span class="hljs-built_in" style="color: #e6c07b; line-height: 26px;">test</span><br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">* 如果只有一个 screen 会话,后面的会话名称可以省略不写,如果你不知道,或者忘了会话命令,你可以通过 <strong style="font-weight: bold; color: rgb(71, 193, 168);">screen -ls</strong> 查看当前主机运行的 screen 会有。</p>
<h4 data-tool="mdnice编辑器" style="margin-top: 30px; margin-bottom: 15px; padding: 0px; font-weight: bold; color: black; font-size: 18px;"><span class="prefix" style="display: none;"></span><span class="content">上下分屏</span><span class="suffix" style="display: none;"></span></h4>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">创建一个 screen 会话</p>
<pre class="custom" data-tool="mdnice编辑器" style="margin-top: 10px; margin-bottom: 10px; border-radius: 5px; box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;"><span style="display: block; background: url(https://files.mdnice.com/point.png); height: 30px; width: 100%; background-size: 40px; background-repeat: no-repeat; background-color: #282c34; margin-bottom: -7px; border-radius: 5px; background-position: 10px 10px;"></span><code class="hljs" style="overflow-x: auto; padding: 16px; color: #abb2bf; display: -webkit-box; font-family: Operator Mono, Consolas, Monaco, Menlo, monospace; font-size: 12px; -webkit-overflow-scrolling: touch; padding-top: 15px; background: #282c34; border-radius: 5px;">it@serverc:~$ screen -S <span class="hljs-built_in" style="color: #e6c07b; line-height: 26px;">test</span><br></code></pre>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">按 <kbd>Ctrl</kbd> + <kbd>A</kbd>,然后按 <kbd>Shift</kbd> + <kbd>S</kbd> 开启分屏;</p>
<figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://www.icode9.com/i/ll/?i=20210125214200366.PNG" alt style="display: block; margin: 0 auto; max-width: 100%; box-shadow: 0px 0px 10px #333; border-radius: 5px 5px 5px 5px;"></figure>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">按 <kbd>Ctrl</kbd> + <kbd>A</kbd>,然后再按 <kbd>Tab</kbd>,切换到下面屏幕</p>
<figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://www.icode9.com/i/ll/?i=20210125214211577.PNG" alt style="display: block; margin: 0 auto; max-width: 100%; box-shadow: 0px 0px 10px #333; border-radius: 5px 5px 5px 5px;"></figure>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">但此时下面屏幕什么还没有,你需要通过 <kbd>Ctrl</kbd> + <kbd>A</kbd>,然后按 <kbd>C</kbd> 在下面屏幕中创建终端</p>
<figure data-tool="mdnice编辑器" style="margin: 0; margin-top: 10px; margin-bottom: 10px; display: flex; flex-direction: column; justify-content: center; align-items: center;"><img src="https://www.icode9.com/i/ll/?i=20210125214221750.PNG" alt style="display: block; margin: 0 auto; max-width: 100%; box-shadow: 0px 0px 10px #333; border-radius: 5px 5px 5px 5px;"></figure>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">这样我们就可以在下面屏幕执行命令了,如果我们需要在不同的屏幕之间切换,可以通过 按 <kbd>Ctrl</kbd> + <kbd>A</kbd>,然后再按 <kbd>Tab</kbd> 来切换。</p>
<p data-tool="mdnice编辑器" style="padding-top: 8px; padding-bottom: 8px; margin: 0; color: rgb(89,89,89); font-size: 15px; line-height: 2.0;">当需要退出时,我们可以通过 exit 命令退出(在一个屏幕运行 exit 命令后,只是终端退出了,分屏的状态并没有退出,所以你需要通过前面的方法,切换到另一个屏幕上,再次运行 exit 退出)。</p>
</section>
相关文章
- 01-30SolidPDF-Tools-v10 一款好用的pdf,word 互转工具
- 01-30iTerm--比Terminal(终端)更好用的命令行工具
- 01-30介绍一款好用的终端工具 Screen
- 01-30BlueHarvest for Mac是一款运行在Mac平台上简单又好用的磁盘清理工具 Apple Silicon的通用版本
- 01-30工具篇:介绍几个好用的guava工具类
- 01-30一款好用的工具fake-useragent及浏览器代理池
- 01-30工具篇:介绍几个好用的guava工具类
- 01-30工具篇:介绍几个好用的guava工具类
- 01-30做一款简单、高效、好用的文件同步工具
- 01-30好用的终端SSH工具SecureCRT Mac破解版