阅读(2840)
赞(8)
9.3.1 管理远程会话
2019-05-05 13:13:40 更新
screen命令能做的事情非常多:可以用-S参数创建会话窗口;用-d参数将指定会话进行离线处理;用-r参数恢复指定会话;用-x参数一次性恢复所有的会话;用-ls参数显示当前已有的会话;以及用-wipe参数把目前无法使用的会话删除,等等。
下面创建一个名称为backup的会话窗口。请各位读者留心观察,当在命令行中敲下这条命令的一瞬间,屏幕会快速闪动一下,这时就已经进入screen服务会话中了,在里面运行的任何操作都会被后台记录下来。
[root@linuxprobe ~]# screen -S backup
[root@linuxprobe ~]#
执行命令后会立即返回一个提示符。虽然看起来与刚才没有不同,但实际上可以查看到当前的会话正在工作中。
[root@linuxprobe ~]# screen -ls
There is a screen on:
32230.backup (Attached)
1 Socket in /var/run/screen/S-root.
要想退出一个会话也十分简单,只需在命令行中执行exit命令即可。
[root@linuxprobe ~]# exit
[screen is terminating]
在日常的生产环境中,其实并不是必须先创建会话,然后再开始工作。可以直接使用screen命令执行要运行的命令,这样在命令中的一切操作也都会被记录下来,当命令执行结束后screen会话也会自动结束。
[root@linuxprobe ~]# screen vim memo.txt
welcome to linuxprobe.com
为了演示screen不间断会话服务的强大之处,我们先来创建一个名为linux的会话,然后强行把窗口关闭掉(这与进行远程连接时突然断网具有相同的效果):
[root@linuxprobe ~]# screen -S linux
[root@linuxprobe ~]#
[root@linuxprobe ~]# tail -f /var/log/messages
Feb 20 11:20:01 localhost systemd: Starting Session 2 of user root.
Feb 20 11:20:01 localhost systemd: Started Session 2 of user root.
Feb 20 11:21:19 localhost dbus-daemon: dbus[1124]: [system] Activating service name='com.redhat.SubscriptionManager' (using servicehelper)
Feb 20 11:21:19 localhost dbus[1124]: [system] Activating service name='com.redhat.SubscriptionManager' (using servicehelper)
Feb 20 11:21:19 localhost dbus-daemon: dbus[1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager'
Feb 20 11:21:19 localhost dbus[1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager'
Feb 20 11:30:01 localhost systemd: Starting Session 3 of user root.
Feb 20 11:30:01 localhost systemd: Started Session 3 of user root.
Feb 20 11:30:43 localhost systemd: Starting Cleanup of Temporary Directories...
Feb 20 11:30:43 localhost systemd: Started Cleanup of Temporary Directories.
由于刚才关闭了会话窗口,这样的操作在传统的远程控制中一定会导致正在运行的命令也突然终止,但在screen不间断会话服务中则不会这样。我们只需查看一下刚刚离线的会话名称,然后尝试恢复回来就可以继续工作了:
[root@linuxprobe ~]# screen -ls
There is a screen on:
13469.linux (Detached)
1 Socket in /var/run/screen/S-root.
[root@linuxprobe ~]# screen -r linux
[root@linuxprobe ~]#
[root@linuxprobe ~]# tail -f /var/log/messages
Feb 20 11:20:01 localhost systemd: Starting Session 2 of user root.
Feb 20 11:20:01 localhost systemd: Started Session 2 of user root.
Feb 20 11:21:19 localhost dbus-daemon: dbus[1124]: [system] Activating service name='com.redhat.SubscriptionManager' (using servicehelper)
Feb 20 11:21:19 localhost dbus[1124]: [system] Activating service name='com.redhat.SubscriptionManager' (using servicehelper)
Feb 20 11:21:19 localhost dbus-daemon: dbus[1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager'
Feb 20 11:21:19 localhost dbus[1124]: [system] Successfully activated service 'com.redhat.SubscriptionManager'
Feb 20 11:30:01 localhost systemd: Starting Session 3 of user root.
Feb 20 11:30:01 localhost systemd: Started Session 3 of user root.
Feb 20 11:30:43 localhost systemd: Starting Cleanup of Temporary Directories...
Feb 20 11:30:43 localhost systemd: Started Cleanup of Temporary Directories.
Feb 20 11:40:01 localhost systemd: Starting Session 4 of user root.
Feb 20 11:40:01 localhost systemd: Started Session 4 of user root.
如果我们突然又想到了还有其他事情需要处理,也可以多创建几个会话窗口放在一起使用。如果这段时间内不再使用某个会话窗口,可以把它设置为临时断开(detach)模式,随后在需要时再重新连接(attach)回来即可。这段时间内,在会话窗口内运行的程序会继续执行。