分页: 1 / 1

进程处理

发表于 : 2008-11-06 16:00
eexpress
事先在命令前加上 nohup 或者 setsid 就可以避免 HUP 信号的影响。但是如果我们未加任何处理就已经提交了命令,该如何补救才能让它避免 HUP 信号的影响呢?

解决方法:

这时想加 nohup 或者 setsid 已经为时已晚,只能通过作业调度和 disown 来解决这个问题了。

我们可以用如下方式来达成我们的目的。
灵活运用 CTRL-z
在我们的日常工作中,我们可以用 CTRL-z 来将当前进程挂起到后台暂停运行,执行一些别的操作,然后再用 fg 来将挂起的进程重新放回前台(也可用 bg 来将挂起的进程放在后台)继续运行。这样我们就可以在一个终端内灵活切换运行多个任务,这一点在调试代码时尤为有用。因为将代码编辑器挂起到后台再重新放回时,光标定位仍然停留在上次挂起时的位置,避免了重新定位的麻烦。

用disown -h jobspec 来使某个作业忽略HUP信号。
用disown -ah 来使所有的作业都忽略HUP信号。
用disown -rh 来使正在运行的作业忽略HUP信号。

disown 示例1(如果提交命令时已经用“&”将命令放入后台运行,则可以直接使用“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]#   

Re: 进程处理

发表于 : 2008-11-06 16:15
sevk
太好了,不知不觉总是感觉到LINUX的强大.

Re: 进程处理

发表于 : 2008-11-07 11:14
aBiNg
不像阿姨文风啊,估计是转的。lol

Re: 进程处理

发表于 : 2008-11-07 20:02
cnkilior
表说的那么直白嘛!

Re: 进程处理

发表于 : 2008-11-07 20:20
yjcong
连电话都没有, 而且是用的root, 想想就 是转的。 估计阿姨最近善良的心灵被小小地刺痛了。 开始多发帖子了 :em05

Re: 进程处理

发表于 : 2008-11-08 2:14
windwiny
看起来也是 转的....看来要多看看 man bash 了,

几乎每时每刻都在用的东西,却几乎没看过它的"男人"...

Re: 进程处理

发表于 : 2008-11-10 10:07
eexpress
罗嗦什么,有电话就是我写的。
这是ibm的文章。man不到的。才转给你们看。

Re: 进程处理

发表于 : 2008-11-10 23:02
bones7456
:em11 阿姨转得好~