时已经用“&”将命令放入后台运行,则可以直接使用“disown”)
[root@pvcent107 build]# cp -r testLargeFile largeFile & [1] 4825 [root@pvcent107 build]# jobs [1] Running cp -i -r testLargeFile largeFile & [root@pvcent107 build]# disown -h %1 [root@pvcent107 build]# ps -ef |grep largeFile root 4825 968 1 09:46 pts/4 00:00:00 cp -i -r testLargeFile largeFile root 4853 968 0 09:46 pts/4 00:00:00 grep largeFile [root@pvcent107 build]# logout |
disown 示例2(如果提交命令时未使用“&”将命令放入后台运行,可使用 CTRL-z 和“bg”将其放入后台,再使用“disown”)
[root@pvcent107 build]# cp -r testLargeFile largeFile2
[1] Stopped cp -i -r testLargeFile largeFile2 [root@pvcent107 build]# bg %1 [1] cp -i -r testLargeFile largeFile2 & [root@pvcent107 build]# jobs [1] Running cp -i -r testLargeFile largeFile2 & [root@pvcent107 build]# disown -h %1 [root@pvcent107 build]# ps -ef |grep largeFile2 root 5790 5577 1 10:04 pts/3 00:00:00 cp -i -r testLargeFile largeFile2 root 5824 5577 0 10:05 pts/3 00:00:00 grep largeFile2 [root@pvcent107 build]# |
screen
场景:
我们已经知道了如何让进程免受 HUP 信号的影响,但是如果有大量这种命令需要在稳定的后台里运行,如何避免对每条命令都做这样的操作呢?
解决方法:
此时最方便的方法就是 screen 了.简单的说,screen 提供了 ANSI/VT100 的终端模拟器,使它能够在一个真实终端下运行多个全屏的伪终端.screen 的参数很多,具有很强大的功能,我们在此仅介绍其常用功能以及简要分析一下为什么使用 screen 能够避免 HUP 信号的影响.我们先看一下 screen 的帮助信息:
SCREEN(1) SCREEN(1)
NAME screen - screen manager with VT100/ANSI terminal emulation
SYNOPSIS screen [ -options ] [ cmd [ args ] ] screen -r [[pid.]tty[.host]] screen -r sessionowner/[[pid.]tty[.host]]
DESCRIPTION Screen is a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells). Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets). There is a scrollback history buffer for each virtual terminal and a copy-and-paste mechanism that allows moving text regions between windows. |
使用 screen 很方便,有以下几个常用选项:
用screen -dmS session name 来建立一个处于断开模式下的会话(并指定其会话名). 用screen -list 来列出所有会话. 用screen -r session name 来重新连接指定会话. 用快捷键CTRL-a d 来暂时断开当前会话.
screen 示例
[root@pvcent107 ~]# screen -dmS Urumchi [root@pvcent107 ~]# screen -list There is a screen on: 12842.Urumchi (Detached) 1 Socket in /tmp/screens/S-root.
[root@pvcent107 ~]# screen -r Urumchi |
|