本文最后更新于 319 天前,其中的信息可能已经过时,如有错误请发送邮件到wuxianglongblog@163.com
screen命令
可以用这个命令来实现程序的后台运行,这样即使服务器连接断了,程序也能继续运行。
[root@localhost ~]# screen //第一次使用需要安装
-bash: screen: command not found
[root@localhost ~]# yum install screen //使用yum安装
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.ustc.edu.cn
* updates: mirrors.ustc.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package screen.x86_64 0:4.1.0-0.27.20120314git3c2946.el7_9 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=================================================================================================
Package Arch Version Repository Size
=================================================================================================
Installing:
screen x86_64 4.1.0-0.27.20120314git3c2946.el7_9 updates 553 k
Transaction Summary
=================================================================================================
Install 1 Package
Total download size: 553 k
Installed size: 914 k
Is this ok [y/d/N]: y
Downloading packages:
screen-4.1.0-0.27.20120314git3c2946.el7_9.x86_64.rpm | 553 kB 00:00:02
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : screen-4.1.0-0.27.20120314git3c2946.el7_9.x86_64 1/1
Verifying : screen-4.1.0-0.27.20120314git3c2946.el7_9.x86_64 1/1
Installed:
screen.x86_64 0:4.1.0-0.27.20120314git3c2946.el7_9
Complete!
[root@localhost ~]#
Screen命令参数:
-A -[r|R] 将所有的视窗都调整为目前终端机的大小。
-c filename 用指定的filename文件替代screen的配置文件’.screenrc’.
-d [pid.tty.host] 断开screen进程(使用该命令时,screen的状态一定要是Attached,也就是说有用户连在screen里)。一般进程的名字是以pid.tty.host这种形式表示(用screen -list命令可以看出状态)。
-D [pid.tty.host] 与-d命令实现一样的功能,区别就是如果执行成功,会踢掉原来在screen里的用户并让他logout。
-h <行数> 指定视窗的缓冲区行数。
-ls或–list 显示目前所有的screen作业。
-m 即使目前已在作业中的screen作业,仍强制建立新的screen作业。
-p number or name 预先选择一个窗口。
-r [pid.tty.host] 恢复离线的screen进程,如果有多个断开的进程,需要指定[pid.tty.host]
-R 先试图恢复离线的作业。若找不到离线的作业,即建立新的screen作业。
-s shell 指定建立新视窗时,所要执行的shell。
-S <作业名称> 指定screen作业的名称。(用来替代[pid.tty.host]的命名方式,可以简化操作).
-v 显示版本信息。
-wipe 检查目前所有的screen作业,并删除已经无法使用的screen作业。
-x 恢复之前离线的screen作业。
screen命令:
1、创建新screen会话,用参数-S
screen –S [SESSION]
[root@localhost ~]# who
root pts/0 2024-01-09 19:34 (192.168.198.1)//当前是pts/0
[root@localhost ~]# screen -S test /创建第一个终端会话
[root@localhost ~]# who
root pts/1 2024-01-09 19:38 (192.168.198.1:S.0) //当前是pts/1
[root@localhost ~]# screen -ls //显示所有已经打开的screen会话
There is a screen on:
8144.test (Attached)
1 Socket in /var/run/screen/S-root.
[root@localhost ~]# screen -S test2 /创建第二个终端会话
[root@localhost ~]# who
root pts/2 2024-01-09 19:39 (192:S.0)
[root@localhost ~]# screen -ls ///显示所有已经打开的screen会话
There are screens on:
8157.test2 (Attached)
8144.test (Attached)
2 Sockets in /var/run/screen/S-root.
[root@localhost ~]#
加入screen会话
screen –x [SESSION]
显示所有已经打开的screen会话
screen -ls
2、恢复某screen会话,使用参数-r或者-x,如果会话是(Attached)状态,需要加-d选项,表示断开screen进程,因为1个screen无法同时打开2次。
screen -r [SESSION]
[root@localhost ~]# screen -ls
There are screens on:
8157.test2 (Attached)
8144.test (Attached)
2 Sockets in /var/run/screen/S-root.
[root@localhost ~]# screen -r test
There are screens on:
8157.test2 (Attached)
8144.test (Attached)
There is no screen to be resumed matching test.//无法进入
[root@localhost ~]# screen -dr test //加-d进入成功
[root@localhost ~]# screen -ls
There are screens on:
8157.test2 (Attached)
8144.test (Attached)
2 Sockets in /var/run/screen/S-root.
[root@localhost ~]# who am i
root pts/2 2024-01-09 19:39 (192:S.0)
[root@localhost ~]#
退出并关闭screen会话
Exit
[root@localhost ~]# screen -ls
There are screens on:
8157.test2 (Attached)
8144.test (Attached)
2 Sockets in /var/run/screen/S-root.
[root@localhost ~]# who am i
root pts/2 2024-01-09 19:39 (192:S.0)
[root@localhost ~]# exit
exit
[screen is terminating]
[root@localhost ~]# screen -ls //只剩下一个,退出成功
There is a screen on:
8144.test (Attached)
1 Socket in /var/run/screen/S-root.
[root@localhost ~]#
3、退出当前screen会话。
Ctrl+D # 在当前screen下,输入Ctrl+D,删除该screen
Ctrl+A,Ctrl+D # 在当前screen下,输入先后Ctrl+A,Ctrl+D,退出该screen
[detached from 8144.test] //退出成功
[root@localhost ~]#
[root@localhost ~]# screen -ls
There is a screen on:
8144.test (Detached)
1 Socket in /var/run/screen/S-root.
[root@localhost ~]# screen -r test //进入会话,(Detached)状态只要-r参数或者使用-x参数
[root@localhost ~]# who
root pts/1 2024-01-09 19:38 (192.168.198.1:S.0)
[root@localhost ~]# screen -ls
There is a screen on:
8144.test (Attached)
1 Socket in /var/run/screen/S-root.
[root@localhost ~]#