disown的作用

零七年的夏天
disown的作用

用disown -h jobspec 来使某个作业忽略HUP信号。


但是我执行
cp -r testLargeFile largeFile &
之后,断开我的网线之后,发现进程还在运行,但是这个时候用disown有什么作用?

waker
你断的是哪条网线?和cp所在的终端有没有联系?你的cp是不是在终端上运行?

零七年的夏天
[quote]原帖由 [i]waker[/i] 于 2008-6-30 14:38 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8703044&ptid=1184542][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
你断的是哪条网线?和cp所在的终端有没有联系?你的cp是不是在终端上运行? [/quote]

我是在自己的笔记本上用SSH连接服务器开了1个窗口来执行这个脚本的,运行后直接把笔记本的网线断开1分钟的,发现这个语句对应的作业还在运行

waker
disown -h是保证程序不收到HUP信号,没有机制保证你拔网线程序就一定收到HUP,也没规定一个程序收到了HUP信号就作出什么反应,也没规定一个程序收到了HUP在什么时候反应

零七年的夏天
[quote]原帖由 [i]waker[/i] 于 2008-6-30 15:51 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8703708&ptid=1184542][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
disown -h是保证程序不收到HUP信号,没有机制保证你拔网线程序就一定收到HUP,也没规定一个程序收到了HUP信号就作出什么反应,也没规定一个程序收到了HUP在什么时候反应 [/quote]

    我的意思是,本来disown就是让程序不收到HUP信号,比如说如果网络断掉,就不给CLINET端发这个信息,这样保证脚本继续执行

   但是我不明白,我执行
   cp -r testLargeFile largeFile &

    这个用&已经放到后台,为什么还要使用disown来屏蔽HUP信号(可能这个屏蔽用的不是很恰当)

waker
[url]http://bbs.chinaunix.net/viewthread.php?tid=1118626&highlight=%BC%C5%C4%AF%C1%D2%BB%F0[/url]

[url]http://bbs.chinaunix.net/thread-1159425-1-1.html[/url]

零七年的夏天
[quote]原帖由 [i]waker[/i] 于 2008-6-30 16:49 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8704301&ptid=1184542][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
[url]http://bbs.chinaunix.net/viewthread.php?tid=1118626&highlight=%BC%C5%C4%AF%C1%D2%BB%F0[/url]
[/quote]

  谢谢,我今天的例子就是看了这篇文章才想起来的,连参数都没有变 :mrgreen:

文章中有这么一句

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


    我就是不明白,为什么都放到后台了,还用disown来屏蔽?

walkerxk
disown用途是去掉脚本的所有者的概念,让你不能再用fg来放到前台,不能用jobs看到,不能kill %1来kill掉,和是不是放入后台没有关系。
man bash
disown [-ar] [-h] [jobspec ...]
       Without options, each jobspec is removed from the table of active  jobs.   If  the  -h  option  is
       given, each jobspec is not removed from the table, but is marked so that SIGHUP is not sent to the
       job if the shell receives a SIGHUP.  If no jobspec is present, and  neither  the  -a  nor  the  -r
       option  is  supplied,  the current job is used.  If no jobspec is supplied, the -a option means to
       remove or mark all jobs; the -r option without a jobspec argument restricts operation  to  running
       jobs.  The return value is 0 unless a jobspec does not specify a valid job.

零七年的夏天
[quote]原帖由 [i]walkerxk[/i] 于 2008-6-30 20:05 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8705495&ptid=1184542][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
disown用途是去掉脚本的所有者的概念,让你不能再用fg来放到前台,不能用jobs看到,不能kill %1来kill掉,和是不是放入后台没有关系。
man bash
disown [-ar] [-h] [jobspec ...]
       Without options, e ... [/quote]

呵呵,也就是说用了DISOWN就不能FG,不能用JOBS按,不能KILL掉,好,下面就是我自己的测试部分

sh test4.sh &
[1] 29537
$ jobs
[1]+  Running                 sh test4.sh &
$ disown -h 1
-bash: disown: 1: no such job
$ disown -h %1
$ fg
sh test4.sh

[1]+  Stopped                 sh test4.sh
$ bg
[1]+ sh test4.sh &
$ jobs
[1]+  Running                 sh test4.sh &
$ kill -9 %1
$ ps -ef | grep test4.sh
db       29583 25496  0 20:38 pts/8    00:00:00 grep test4.sh
[1]+  Killed                  sh test4.sh


一个进程,我用了DISOWN后,发现能JOBS,能FG,能KILL,

walkerxk
你的shell是什么环境?什么版本的?
我的:
GNU bash, version 3.00.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.
你看看你的man bash,看看disown部分是不是和我一样。

零七年的夏天
[quote]原帖由 [i]walkerxk[/i] 于 2008-6-30 20:29 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8705630&ptid=1184542][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
你的shell是什么环境?什么版本的?
我的:
GNU bash, version 3.00.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.
你看看你的man bash,看看disown部分是不是和 ... [/quote]

我不知道怎么看具体的SHELL版本

echo $SHELL
/bin/bash


不过我没有MAN出来,
man bash | grep disown

walkerxk
bash --version
man bash|grep disown我这也没有,我是man bash,然后在里面搜索的。或者你PAGER='less -p disown' man bash,然后按n查找下一个。

sway2004009
[quote]原帖由 [i]零七年的夏天[/i] 于 2008-6-30 19:53 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8705434&ptid=1184542][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]


  谢谢,我今天的例子就是看了这篇文章才想起来的,连参数都没有变 :mrgreen:

文章中有这么一句

disown 示例1(如果提交命令时已经用“&”将命令放入后台运行,则可以直接使用“disown”)
        ... [/quote]


收到sighup信号,跟放在后台没关系啊。
放在后台并不影响程序收到sighup信号。
哪里规定了程序放在后台了,就不能收到sighup信号了?

零七年的夏天
[quote]原帖由 [i]sway2004009[/i] 于 2008-7-1 01:00 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8706796&ptid=1184542][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]



收到sighup信号,跟放在后台没关系啊。
放在后台并不影响程序收到sighup信号。
哪里规定了程序放在后台了,就不能收到sighup信号了? [/quote]



  那你是怎么理解的?我对这个理解不正确

walkerxk
[quote]原帖由 [i]零七年的夏天[/i] 于 2008-7-1 12:36 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8709935&ptid=1184542][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]




  那你是怎么理解的?我对这个理解不正确 [/quote]
我晕。
这俩者根本没有关系,就好像你坐着车闯红灯要罚款,走路闯红灯也要罚款,只是你运动的方式不同,和是否罚款没有关系。

零七年的夏天
[quote]原帖由 [i]walkerxk[/i] 于 2008-6-30 21:27 发表 [url=http://bbs.chinaunix.net/redirect.php?goto=findpost&pid=8705969&ptid=1184542][img]http://bbs.chinaunix.net/images/common/back.gif[/img][/url]
bash --version
man bash|grep disown我这也没有,我是man bash,然后在里面搜索的。或者你PAGER='less -p disown' man bash,然后按n查找下一个。 [/quote]

bash --version
GNU bash, version 3.00.15(1)-release (i386-redhat-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.

The shell exits by default upon receipt of a SIGHUP.  Before  exiting,
       an  interactive  shell  resends  the  SIGHUP  to  all jobs, running or
       stopped.  Stopped jobs are sent SIGCONT to ensure  that  they  receive
       the SIGHUP.  To prevent the shell from sending the signal to a partic-
       ular job, it should be removed from the jobs  table  with  the  disown
       builtin  (see  SHELL  BUILTIN COMMANDS below) or marked to not receive
       SIGHUP using disown -h.

waker
shell  resends  the  SIGHUP  to  all jobs
请读50遍

seeLnd
做了个实验[code]~$ cat test.sh
#!/bin/sh
trap "echo sighup >/tmp/sig" HUP
sleep 10

~$ rm -f /tmp/sig
~$ ./test.sh & disown -h %1 ; sleep 2; kill -SIGHUP $$
~$ cat /tmp/sig
cat: /tmp/sig: No such file or directory

~$ rm -f /tmp/sig
~$ ./test.sh & sleep 2; kill -SIGHUP $$
~$ cat /tmp/sig
sighup[/code]

[[i] 本帖最后由 seeLnd 于 2008-7-1 14:23 编辑 [/i]]