screen命令详解

cuixiaogang

在linux的使用中,如果一个长时间运行的任务,在运行期间关掉窗口或者断开连接,此任务将会被杀掉,一切半途而废。让我们了解一下为什么会出现这种状况。

出现此状况的原因

在linux/unix中,有以下几个概念

  • 进程组(process group):一个或多个进程的集合,每个进程组有唯一一个进程组ID,即进程组长的进程ID
  • 会话期(session):一个或多个进程组的集合,有唯一一个会话期首进程(session leader)。会话期ID为首进程的ID
  • 每个会话期可以有一个单独的控制终端(controlling terminal)。与控制终端连接的会话期首进程叫做控制进程(controlling process)。当前与终端交互的进程称为前台进程组。其余进程组称为后台进程组。

根据POSIX.1定义

  • 挂断信号(SIGHUP)默认的动作是终止程序
  • 当终端接口检测到网络连接断开,将挂断的信号发送给控制进程(会话期首进程)
  • 如果会话期首进程终止,则该信号发送到该会话的前台进程组
  • 一个进程退出导致一个孤儿进程组中产生时,如果任意一个孤儿进程组出于STOP状态没法送SIGHUP和SIGGONT信号到该进程组中的所有进程。

nohup命令

如果我们需要忽略SIGHUP信号,关掉窗口应该就不会影响程序的运行了。nohup命令可以达到这个目的,如果程序的标准输出/标准错误是终端,nohup默认将其重定向到nohup.out文件。值得注意的是nohup命令只是使得程序忽略SIGHUP信号,还需要使用标记&把它放在后台运行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[cuixiaogang@bjo18-176-consoledev back-api-gang.dianhua.cn]$ nohup --help
Usage: nohup COMMAND [ARG]...
or: nohup OPTION
Run COMMAND, ignoring hangup signals.

--help display this help and exit
--version output version information and exit

If standard input is a terminal, redirect it from /dev/null.
If standard output is a terminal, append output to 'nohup.out' if possible,
'$HOME/nohup.out' otherwise.
If standard error is a terminal, redirect it to standard output.
To save output to FILE, use 'nohup COMMAND > FILE'.

NOTE: your shell may have its own version of nohup, which usually supersedes
the version described here. Please refer to your shell's documentation
for details about the options it supports.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'nohup invocation'

scree命令

Screen是一个可以在多个进程之间多路复用一个物理终端的窗口管理器。Screen中有会话的概念,用户可以在一个screen会话中创建多个screen窗口,在每一个screen窗口中就像操作一个真实的telnet/SSH连接窗口那样。

简单来说,Screen可以做三种事情

  • 会话的恢复
  • 多窗口
  • 会话的共享
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[cuixiaogang@bjo18-176-consoledev back-api-gang.dianhua.cn]$ screen --help
Use: screen [-opts] [cmd [args]]
or: screen -r [host.tty]

Options:
-4 Resolve hostnames only to IPv4 addresses.
-6 Resolve hostnames only to IPv6 addresses.
-a Force all capabilities into each window's termcap.
-A -[r|R] Adapt all windows to the new display width & height.
-c file Read configuration file instead of '.screenrc'.
-d (-r) Detach the elsewhere running screen (and reattach here).
-dmS name Start as daemon: Screen session in detached mode.
-D (-r) Detach and logout remote (and reattach here).
-D -RR Do whatever is needed to get a screen session.
-e xy Change command characters.
-f Flow control on, -fn = off, -fa = auto.
-h lines Set the size of the scrollback history buffer.
-i Interrupt output sooner when flow control is on.
-l Login mode on (update /var/run/utmp), -ln = off.
-ls [match] or
-list Do nothing, just list our SockDir [on possible matches].
-L Turn on output logging.
-m ignore $STY variable, do create a new screen session.
-O Choose optimal output rather than exact vt100 emulation.
-p window Preselect the named window if it exists.
-q Quiet startup. Exits with non-zero return code if unsuccessful.
-Q Commands will send the response to the stdout of the querying process.
-r [session] Reattach to a detached screen process.
-R Reattach if possible, otherwise start a new session.
-s shell Shell to execute rather than $SHELL.
-S sockname Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
-t title Set title. (window's name).
-T term Use term as $TERM for windows, rather than "screen".
-U Tell screen to use UTF-8 encoding.
-v Print "Screen version 4.01.00devel (GNU) 2-May-06".
-wipe [match] Do nothing, just clean up SockDir [on possible matches].
-x Attach to a not detached screen. (Multi display mode).
-X Execute <cmd> as a screen command in the specified session.

常用命令

  • screen:创建一个新的会话窗口
  • screen [cmd] :创建一个新的会话窗口并执行cmd命令
  • 在screen创建的会话窗口中使用C+a c创建新的窗口(先按Ctrl+a,然后按c)
  • 在screen创建的会话窗口中使用C+a d暂时中断会话
  • screen -ls 查询所有暂时中断的会话
  • screen -wipe 清除已经死掉的会话
1
2
3
4
There are screens on:
15758.pts-2.izm5eiqbgyn5z3c15j5nedz (Attached)
15739.pts-2.izm5eiqbgyn5z3c15j5nedz (Detached)
15655.pts-2.izm5eiqbgyn5z3c15j5nedz (Detached)
  • screen -r [pid] 恢复中断的会话(pid=15758、15739 、15655)
  • 常用的C+a快捷键

常用C+a快捷键
常用C+a快捷键