linux系统中不间断会话服务screen命令
screen命令解决的问题:
用户可以通过该软件同时连接多个本地或远程的命令行会话,并在其间自由切换。
如果一些程序需要运行很长时间,而程序运行过程中出现网络故障,或者客户机故障,这时候客户机与远程服务器的链接将终端,并且远程服务器没有正常结束的命令将被迫终止(可以使用screen避免这种情况)。
即便网络中断会话也可以恢复。
基本用法:
1、创建screen窗口,screen -S name
[root@linuxprobe test]# whoami
root
[root@linuxprobe test]# pwd
/root/test
[root@linuxprobe test]# screen -S test1 ## 没有安装screen
-bash: /usr/bin/screen: No such file or directory
使用yum安装screen,前提是已经配置好yum仓库。
[root@linuxprobe test]# yum install screen -y
[root@linuxprobe test]# screen --version ## 已安装成功
Screen version 4.01.00devel (GNU) 2-May-06
[root@linuxprobe test]# which screen
/usr/bin/screen
创建一个screen窗口:
[root@linuxprobe test]# screen -S test1 ## 创建窗口
[root@linuxprobe test]# exit ## 退出screen窗口或者ctrl a + d(放置后台)
2、创建scrren窗口,运行程序,然后放置后台,看能否恢复窗口
[root@linuxprobe test]# screen -S test1 ## 创建名为test1的窗口
[root@linuxprobe test]# seq 300000000 | sort | uniq -c > a.txt ## 执行一个耗时程序
ctrl a + d ## 先按ctrl和a,再按d,将该窗口放置后台
3、如何查看已经创建的窗口,比如刚才创建的test1窗口
[root@linuxprobe test]# screen -ls
There is a screen on:
2648.test1 (Detached)
1 Socket in /var/run/screen/S-root.
4、再创建一个screen窗口,执行耗时程序,放置后台
[root@linuxprobe test]# screen -S test2 ## 创建一个名为test2的screen窗口
[root@linuxprobe test]# seq 300000000 | sort | uniq -c > b.txt ## 在该窗口内执行一个耗时程序
ctrl a + d ## 先按ctrl 和a,再按d,将该screen放置后台
5、查看当前提交了多少个screen
[root@linuxprobe test]# screen -ls ## 可见一共递交了两个
There are screens on:
2688.test2 (Detached)
2648.test1 (Detached)
2 Sockets in /var/run/screen/S-root.
6、恢复其中一个screen窗口,查看递交的程序
[root@linuxprobe test]# seq 300000000 | sort | uniq -c > a.txt
7、退出当前的终端,再次登录验证程序是否还在
## 再次登录后,仍然存在
[root@linuxprobe ~]# screen -ls
There are screens on:
2688.test2 (Detached)
2648.test1 (Detached)
2 Sockets in /var/run/screen/S-root.
[root@linuxprobe ~]# screen -r 32986.test2
[root@linuxprobe test]# seq 300000000 | sort | uniq -c > b.txt
8、如何关闭screen窗口
[root@linuxprobe test]# screen -ls ## 查看当前窗口
There are screens on:
2688.test2 (Detached)
2648.test1 (Detached)
2 Sockets in /var/run/screen/S-root.
[root@linuxprobe test]# screen -X -S 2648.test1 quit ## 关闭窗口1
[root@linuxprobe test]# screen -ls
There is a screen on:
2688.test2 (Detached)
1 Socket in /var/run/screen/S-root.
[root@linuxprobe test]# kill 2688 ## 可以直接杀掉,关闭test2
[root@linuxprobe test]# screen -ls
No Sockets found in /var/run/screen/S-root.
9、重新创建窗口,断网,检测程序是否会断
[root@linuxprobe test]# screen -S test
[root@linuxprobe test]# seq 300000000 | sort | uniq -c > c.txt
ctrl a + d ## 放置后台
[root@linuxprobe test]# screen -ls
There is a screen on:
2993.test (Detached)
1 Socket in /var/run/screen/S-root.
断网:
测试网络连接:
[root@linuxprobe test]# ping -c 3 www.baidu.com
ping: unknown host www.baidu.com
30s后开启:
测试网路连接:
[root@linuxprobe test]# ping -c 3 www.baidu.com
PING www.a.shifen.com (39.156.66.14) 56(84) bytes of data.
64 bytes from 39.156.66.14: icmp_seq=1 ttl=51 time=16.7 ms
64 bytes from 39.156.66.14: icmp_seq=2 ttl=51 time=16.5 ms
64 bytes from 39.
检查程序,程序依然运行
[root@linuxprobe test]# screen -ls There is a screen on: 2993.test (Detached) 1 Socket in /var/run/screen/S-root. [root@linuxprobe test]# screen -r 2993.test [root@linuxprobe test]# seq 300000000 | sort | uniq -c > c.txt
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律