linux命令nohup LInux命令设置参数( 五 )


回时,光标定位仍然停留在上次挂起时的位置,避免了重新定位的麻烦 。
用disown -h jobspec来使某个作业忽略HUP信号 。
用disown -ah 来使所有的作业都忽略HUP信号 。
用disown -rh 来使正在运行的作业忽略HUP信号 。
需要注意的是,当使用过 disown 之后 , 会将把目标作业从作业列表中移除 , 我们将不能再使用jobs来查看它,但是依然能够用ps -ef查找到它 。
但是还有一个问题,这种方法的操作对象是作业,如果我们在运行命令时在结尾加了""来使它成为一个作业并在后台运行,那么就万事大吉了,我们可以通过jobs命令来得到所有作业的列表 。但是如果并没有把当前命令作为作业来运行,如何才能得到它的作业号呢?答案就是用 CTRL-z(按住Ctrl键的同时按住z键)了!
CTRL-z 的用途就是将当前进程挂起(Suspend) , 然后我们就可以用jobs命令来查询它的作业号,再用bg jobspec来将它放入后台并继续运行 。需要注意的是,如果挂起会影响当前进程的运行结果,请慎用此方法 。
disown 示例1(如果提交命令时已经用“”将命令放入后台运行,则可以直接使用“disown”)
[root@pvcent107 build]# cp -r testLargeFile largeFile
[1] 4825
[root@pvcent107 build]# jobs
[1]+Runningcp -i -r testLargeFile largeFile
[root@pvcent107 build]# disown -h %1
[root@pvcent107 build]# ps -ef |grep largeFile
root48259681 09:46 pts/400:00:00 cp -i -r testLargeFile largeFile
root48539680 09:46 pts/400:00:00 grep largeFile
[root@pvcent107 build]# logout
disown 示例2(如果提交命令时未使用“”将命令放入后台运行 , 可使用 CTRL-z 和“bg”将其放入后台,再使用“disown”)
[root@pvcent107 build]# cp -r testLargeFile largeFile2
[1]+Stoppedcp -i -r testLargeFile largeFile2
[root@pvcent107 build]# bg %1
[1]+ cp -i -r testLargeFile largeFile2
[root@pvcent107 build]# jobs
[1]+Runningcp -i -r testLargeFile largeFile2
[root@pvcent107 build]# disown -h %1
[root@pvcent107 build]# ps -ef |grep largeFile2
root579055771 10:04 pts/300:00:00 cp -i -r testLargeFile largeFile2
root582455770 10:05 pts/300: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
Screenisafull-screenwindow manager that multiplexes a physical
terminal between severalprocesses(typicallyinteractiveshells).
Eachvirtualterminal provides the functions of a DEC VT100 terminal
and, in addition, several control functions from theISO6429(ECMA
48,ANSIX3.64)and ISO 2022 standards (e.g. insert/delete line and
support for multiple character sets).There is ascrollbackhistory
bufferforeach virtual terminal and a copy-and-paste mechanism that
allows moving text regions between windows.
使用 screen 很方便,有以下几个常用选项:

推荐阅读