这篇文章主要为大家展示了“linux中如何使用屏幕”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“linux中如何使用屏幕”这篇文章吧。
屏幕是一款由GNU开发的命令行终端工具,它提供了从多个终端窗口连接到同一个shell会话(会话共享)。当网络中断,或终端窗口意外关闭是,中屏幕中运行的程序任然可以运行(系统自带的终端窗口,当窗口意外关闭时,在该终端窗口中运行的程序也会终止)。
<强>安装屏幕工具强>
it@serverc: ~,美元sudo apt install screen - y
*,在 CentOS 中,你需要先安装 epel-release 才可以安装 screen( 在 CentOS 中安装软件使用 yum 或者 dnf )
会话共享
在 A 主机上创建一个名为 it 的 screen 会话
it@serverc:~$ screen -S it
在 B 主机上,通过 ssh 连接到 A 主机,并查通过 -ls 选项看当前已经存在的 screen 会话
it@workstation:~/ansible$ ssh it@10.10.10.108 it@10.10.10.108's password: Welcome to Ubuntu 20.04.1 LTS (GNU/Linux 5.4.0-60-generic x86_64) ... ... ... ... ... ... ... ... Last login: Thu Jan 14 13:57:32 2021 it@serverc:~$ screen -ls There is a screen on: 2107.it (01/14/21 07:30:27) (Attached) 1 Socket in /run/screen/S-it.
然后加入到该会话
it@serverc:~$ screen -x it
这样就可以两边共享一个 screen 会话,不管谁运行什么命令,另一个都可以看到;
当你需要退出会话时,你可以运行 exit 来退出会话(当运行 exit 时,两边都会退出 screen 会话。),也可以通过Ctrl+A,D(Ctrl + A 然后按 D,退出单个会话)。
当网络中断或窗口意外关闭后,任务继续运行
在 Linux 打开两个终端窗口,我们分部称他们为 A 窗口和 B 窗口,在 A 窗口中运行 ping
it@workstation:~$ ping 10.10.10.1 PING 10.10.10.1 (10.10.10.1) 56(84) bytes of data. 64 bytes from 10.10.10.1: icmp_seq=1 ttl=64 time=1.21 ms 64 bytes from 10.10.10.1: icmp_seq=2 ttl=64 time=1.22 ms 64 bytes from 10.10.10.1: icmp_seq=3 ttl=64 time=1.19 ms ... ... ... ... ... ... ... ...
然后在 B 窗口,运行 ps aux 获取系统进程,然后通过 grep 进行筛选
it@workstation:~$ ps aux | grep ping it 2336 0.0 0.2 317144 8740 ? Ssl 2020 0:17 /usr/libexec/gsd-housekeeping it 153585 0.0 0.0 12764 872 pts/3 S+ 14:53 0:00 ping 10.10.10.1 it 153646 0.0 0.0 12108 736 pts/2 S+ 14:56 0:00 grep --color=auto ping
我们看到 ping 正在运行
当关掉 A 窗口后,我们再到 B 窗口(此时就只有一个窗口了)运行之前的 ps aux 命令
it@workstation:~$ ps aux | grep ping it 2336 0.0 0.2 317144 8740 ? Ssl 2020 0:17 /usr/libexec/gsd-housekeeping it 153651 0.0 0.0 12108 2964 pts/2 S+ 14:57 0:00 grep --color=auto ping
此时 ping 命令已经结束了
重新打开一个新的终端窗口,作为 A 窗口,运行 screen
it@serverc:~$ screen -S test
然后在 screen 窗口中运行 ping 命令
it@workstation:~$ ping 10.10.10.1 PING 10.10.10.1 (10.10.10.1) 56(84) bytes of data. 64 bytes from 10.10.10.1: icmp_seq=1 ttl=64 time=1.21 ms 64 bytes from 10.10.10.1: icmp_seq=2 ttl=64 time=1.15 ms 64 bytes from 10.10.10.1: icmp_seq=3 ttl=64 time=1.21 ms ... ... ... ... ... ... ... ...nulllinux中如何使用屏幕