[Linux Tips] using the screen to manage your remote session (reprint)

 

linux Tips: Use screen to manage your remote session

 

Reprinted Address: https://www.ibm.com/developerworks/cn/linux/l-cn-screen/ 

 

Do you often need to SSH or telnet to the Linux server telent? You are not always as some of the long-running tasks and headaches, such as system backups, ftp transfer, and so on. Normally we are for each of these tasks remotely open a terminal window, because of the time they perform too long. We have to wait for it is finished, during this period can not be turned off or disconnected from the window, otherwise the mission will be killed, everything halfway.

Culprit: SIGHUP signal

Let's look at why the window to switch off / disconnect will make the running program die.

In Linux / Unix, there are the following concepts:

  • Process group (process group): a set of one or more processes, each process group has only one process group ID, i.e., the process ID of the head of the process.
  • SESSION (session): a collection of one or more of the process group, there is only one session of the First Process (session leader). ID ID of the session, led the process.
  • SESSION can have a separate control terminal (controlling terminal). The first session of the process connected to the control terminal called control process (controlling process). Current interact with a process called terminal foreground process group. The remaining process group known as background process group.

According to POSIX.1 definition:

  • Hang up signal (SIGHUP) The default action is to terminate the program.
  • When the network interface detects the terminal disconnection, the hang up signal to the control process (the first process of the session).
  • If the first session of the termination process, the signal is sent to the session of the foreground process group.
  • When a process exits leading to an orphan process group to produce, if any of the orphan process group process is in STOP state, and SIGCONT send SIGHUP signal to all processes in the process group.

So when disconnected from the network or terminal window is closed, the control process receives a SIGHUP signal to exit, it will lead to other processes during the session exit.

We look at an example. Open SSH terminal two windows, in which a running top.

[root@tivf09 root]# top

 

In another terminal window, find the top of the process ID of 5180, its parent process ID is 5128, that is, login shell.

[root@tivf09 root]# ps -ef|grep top
root      5180  5128  0 01:03 pts/0    00:00:02 top
root      5857  3672  0 01:12 pts/2    00:00:00 grep top

 

Use pstree command can more clearly see the relationship:

[root@tivf09 root]# pstree -H 5180|grep top
|-sshd-+-sshd---bash---top
            

 

Use the command ps-xj can see, the login shell (PID 5128) and top in the same session period, shell for the first session of the course, where the process group PGID as 5128, top location for the process group PGID 5180, for the foreground process group.

[root@tivf09 root]# ps -xj|grep 5128
 5126  5128  5128  5128 pts/0     5180 S        0   0:00 -bash
 5128 5180 5180 5128 pts / 0 5180 S 0 0:50 top
 3672 18095 18094 3672 pts / 2 18094 S 0 0:00 grip 5128

 

SSH close the first window, the other window in the top can be seen also been killed.

[root@tivf09 root]# ps -ef|grep 5128
root     18699  3672  0 04:35 pts/2    00:00:00 grep 5128

 

If we can ignore SIGHUP signal, turn off window should be will not affect the running of the program. nohup command can achieve this purpose, if the output of the standard program / standard error is a terminal, redirect it to a default nohup nohup.out file. It is noteworthy that just makes the program nohup command ignores SIGHUP signal, also need to use the mark & put it in the background.

nohup <command> [argument…] &

 

Although nohup is easy to use, but still relatively "primitive", for the simple command can handle it, human-computer interaction need for complex tasks in trouble.

In fact, we can use a more powerful utility screen. Popular Linux distributions (such as Red Hat Enterprise Linux 4) usually comes with screen utility, if not, can be downloaded from the official website GNU screen.

[Root @ tivf06 ~] # rpm -qa | grep screen
xscreensaver-4.18-5.rhel4.11
screen-4.0.2-5
            

 

Start using Screen

Briefly, Screen is a window manager can multiplex a physical terminal among multiple processes. Screen in the concept of a session, users can create multiple screen windows in a screen session, each operating as a screen window in a real telnet / SSH connection window so. Create a new window in the screen in such ways:

1. Type screen commands directly from the command line

[root@tivf06 ~]# screen

 

Screen execution shell of a full-screen window is created. You can execute arbitrary shell program, as in the ssh window. Type exit to exit the window in the window, if this is the only window of the screen session, exit the screen session, otherwise the screen automatically switches to the previous window.

2. Screen command followed by the program you want to perform.

[root@tivf06 ~]# screen vi test.c

 

Screen vi test.c create a single-window session execution, quit vi will exit the window / session.

3. Above two methods are creating a new screen session. We can also create a new window in an existing screen session. Type in the current screen window C-a c, i.e., Ctrl key + a key, and then press c after, to generate a new window screen in the session and to switch to the window.

screen as well as more advanced features. You can not interrupt the operation screen window program is temporarily disconnected (detach) screen session and reconnect at a later time (attach) the session to regain control program running in each window. For example, we open a screen window to edit / tmp / abc file:

[root@tivf06 ~]# screen vi /tmp/abc

 

Then we want to temporarily quit doing something else, like go for a walk, then type in the screen window C-a d, Screen gives detached prompt:


Temporary interruption of the session
Temporary interruption of the session

Half an hour later came back to find that screen session:

[root@tivf06 ~]# screen -ls
There is a screen on:
        16582.pts-1.tivf06      (Detached)
1 Socket in /tmp/screens/S-root.

 

Reconnect the session:

[root@tivf06 ~]# screen -r 16582

 

To see what there was, great, everything. Continue to do it.

You may have noticed that the command is sent to the screen using a special key combination Ca. This is because the information we typed on the keyboard is sent directly to the current screen window, it must be given in other ways to screen window manager command, by default, screen receives a command to Ca began. This form is called a command key bindings (key binding) in the screen, Ca is called the command character (command character).

Can C-a ?view all of the key bindings, commonly used key bindings are:

 

C-a ? Display all key bindings
C-a w 显示所有窗口列表
C-a C-a 切换到之前显示的窗口
C-a c 创建一个新的运行shell的窗口并切换到该窗口
C-a n 切换到下一个窗口
C-a p 切换到前一个窗口(与C-a n相对)
C-a 0..9 切换到窗口0..9
C-a a 发送 C-a到当前窗口
C-a d 暂时断开screen会话
C-a k 杀掉当前窗口
C-a [ 进入拷贝/回滚模式

Screen常用选项

使用键绑定C-a ?命令可以看到, 默认的命令字符(Command key)为C-a,转义C-a(literal ^a)的字符为a:


Screen 常用选项
Screen Commonly Used Options

因为screen把C-a看作是screen命令的开始,所以如果你想要screen窗口接收到C-a字符,就要输入C-a a。Screen也允许你使用-e选项设置自己的命令字符和转义字符,其格式为:

-exy x为命令字符,y为转义命令字符的字符

下面命令启动的screen会话指定了命令字符为C-t,转义C-t的字符为t,通过C-t ?命令可以看到该变化。

[root@tivf18 root]# screen -e^tt



自定义命令字符和转义字符
Custom commands characters and escape characters

其他常用的命令选项有:

 

-c file 使用配置文件file,而不使用默认的$HOME/.screenrc
-d|-D [pid.tty.host] 不开启新的screen会话,而是断开其他正在运行的screen会话
-h num 指定历史回滚缓冲区大小为num行
-list|-ls 列出现有screen会话,格式为pid.tty.host
-d -m 启动一个开始就处于断开模式的会话
-r sessionowner/ [pid.tty.host] 重新连接一个断开的会话。多用户模式下连接到其他用户screen会话需要指定sessionowner,需要setuid-root权限
-S sessionname 创建screen会话时为会话指定一个名字
-v 显示screen版本信息
-wipe [match] 同-list,但删掉那些无法连接的会话

下例显示当前有两个处于detached状态的screen会话,你可以使用screen -r <screen_pid>重新连接上:

[root@tivf18 root]# screen –ls
There are screens on:
        8736.pts-1.tivf18       (Detached)
        8462.pts-0.tivf18       (Detached)
2 Sockets in /root/.screen.

[root@tivf18 root]# screen –r 8736

 

如果由于某种原因其中一个会话死掉了(例如人为杀掉该会话),这时screen -list会显示该会话为dead状态。使用screen -wipe命令清除该会话:

[root@tivf18 root]# kill -9 8462
[root@tivf18 root]# screen -ls  
There are screens on:
        8736.pts-1.tivf18       (Detached)
        8462.pts-0.tivf18       (Dead ???)
Remove dead screens with 'screen -wipe'.
2 Sockets in /root/.screen.

[root@tivf18 root]# screen -wipe
There are screens on:
        8736.pts-1.tivf18       (Detached)
        8462.pts-0.tivf18       (Removed)
1 socket wiped out.
1 Socket in /root/.screen.

[root@tivf18 root]# screen -ls  
There is a screen on:
        8736.pts-1.tivf18       (Detached)
1 Socket in /root/.screen.

[root@tivf18 root]#

 

-d –m 选项是一对很有意思的搭档。他们启动一个开始就处于断开模式的会话。你可以在随后需要的时候连接上该会话。有时候这是一个很有用的功能,比如我们可以使用它调试后台程序。该选项一个更常用的搭配是:-dmS sessionname

启动一个初始状态断开的screen会话:

[root@tivf06 tianq]# screen -dmS mygdb gdb execlp_test

 

连接该会话:

[root@tivf06 tianq]# screen -r mygdb

 

管理你的远程会话

先来看看如何使用screen解决SIGHUP问题,比如现在我们要ftp传输一个大文件。如果按老的办法,SSH登录到系统,直接ftp命令开始传输,之后。。如果网络速度还可以,恭喜你,不用等太长时间了;如果网络不好,老老实实等着吧,只能传输完毕再断开SSH连接了。让我们使用screen来试试。

SSH登录到系统,在命令行键入screen。

[root@tivf18 root]# screen

 

在screen shell窗口中输入ftp命令,登录,开始传输。不愿意等了?OK,在窗口中键入C-a d:


管理你的远程会话
Manage your remote session

然后。。退出SSH登录?随你怎样,只要别杀掉screen会话。

是不是很方便?更进一步,其实我们可以利用screen这种功能来管理你的远程会话,保存你所有的工作内容。你是不是每次登录到系统都要开很多窗口,然后每天都要重复打开关闭这些窗口?让screen来帮你“保存”吧,你只需要打开一个ssh窗口,创建需要的screen窗口,退出的时候C-a d“保存”你的工作,下次登录后直接screen -r <screen_pid>就可以了。

最好能给每个窗口起一个名字,这样好记些。使用C-a A给窗口起名字。使用C-a w可以看到这些窗口名字,可能名字出现的位置不同。使用putty:


putty
putty

使用telnet:


telnet
telnet

更多Screen功能

Screen提供了丰富强大的定制功能。你可以在Screen的默认两级配置文件/etc/screenrc和$HOME/.screenrc中指定更多,例如设定screen选项,定制绑定键,设定screen会话自启动窗口,启用多用户模式,定制用户访问权限控制等等。如果你愿意的话,也可以自己指定screen配置文件。

Multi-user functionality, for example, screen default is single-user mode, you need to specify the multiuser on in the configuration file to open the multi-user mode by acl * (acladd, acldel, aclchg ...) command, you can be flexible Configuring other users access to your screen session. More profiles please refer to the screen man page.

Reference material

Reproduced in: https: //www.cnblogs.com/licheng/archive/2012/06/13/2547225.html

Guess you like

Origin blog.csdn.net/weixin_33989058/article/details/93800119