免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3518 | 回复: 10
打印 上一主题 下一主题

关于后台运行&的一些疑问 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-24 16:41 |只看该作者 |倒序浏览
平时需要在后台执行任务的时候,常使用nohup或者screen之类的东东,确实好用。

不过今天有一个情况有点不解,一直都这么认为
1、nohup command 登录后执行command,logout后,此command不受影响的继续运行
2、command & 把任务放在后台 ,用户logout后,此command也被kill掉

第一点是深信不疑的,对于第二点,发现有这么一种情况。

  1. $ping [url]www.163.com[/url] >>163.txt &
  2. [1] 25236
  3. $ ps -ef |grep ping
  4. sszheng  25236 25224  0 16:31 pts/0    00:00:00 ping [url]www.163.com[/url]
  5. sszheng  25238 25224  0 16:31 pts/0    00:00:00 grep ping
复制代码

很显然他是bash的子进程

  1. $ pstree   
  2. +-sshd---sshd---sshd---bash---ping
  3. ¦                           +-pstre
复制代码

$exit
关闭secureCRT
然后再打开secureCRT,然后登陆

  1. $ ps -ef |grep ping
  2. sszheng  25236     1  0 16:31 ?        00:00:00 ping [url]www.163.com[/url]
  3. sszheng  25250 25243  0 16:34 pts/1    00:00:00 grep ping
  4. 很明显,上一次退出sshd,ping就给了init了,PPID为1。
复制代码

  1. $pstree
  2. +-ping
  3. +-sshd---sshd---sshd---bash---pstree
复制代码


如果是nohup的话,可以很好理解,当用户注销(logout)或者网络断开时,终端会收到 HUP(hangup)信号从而关闭其所有子进程,而nohup会忽略这个hangup 信号信号。

对于&,应该怎么理解?
command & 把任务放在后台 ,用户logout后,此command也被kill掉,怎么体现了?
是不是我理解错什么了?望指教。

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
2 [报告]
发表于 2008-10-24 17:04 |只看该作者

回复 #1 无声无息 的帖子

好问题。
还没注意过。
研究研究。

论坛徽章:
0
3 [报告]
发表于 2008-10-24 17:06 |只看该作者

回复 #2 blackold 的帖子

黑哥,你的环境测试结果是不是一样的?

论坛徽章:
0
4 [报告]
发表于 2008-10-24 17:14 |只看该作者
原帖由 无声无息 于 2008-10-24 16:41 发表
平时需要在后台执行任务的时候,常使用nohup或者screen之类的东东,确实好用。

不过今天有一个情况有点不解,一直都这么认为
1、nohup command 登录后执行command,logout后,此command不受影响的继续运行
2 ...

调用两次fork()就会出现你说的情况
APUE上的一段
  1. If we want to write a process so that it forks a child but we don't want to wait for the child to complete and we don't want the child to become a zombie until we terminate, the trick is to call fork twice. The program in Figure 8.8 does this.

  2. We call sleep in the second child to ensure that the first child terminates before printing the parent process ID. After a fork, either the parent or the child can continue executing; we never know which will resume execution first. If we didn't put the second child to sleep, and if it resumed execution after the fork before its parent, the parent process ID that it printed would be that of its parent, not process ID 1.

  3. Executing the program in Figure 8.8 gives us

  4.    $ ./a.out
  5.    $ second child, parent pid = 1


  6. Note that the shell prints its prompt when the original process terminates, which is before the second child prints its parent process ID.
  7. Figure 8.8. Avoid zombie processes by calling fork twice

  8. #include "apue.h"
  9. #include <sys/wait.h>

  10. int
  11. main(void)
  12. {
  13.     pid_t   pid;

  14.     if ((pid = fork()) < 0) {
  15.         err_sys("fork error");
  16.     } else if (pid == 0) {     /* first child */
  17.         if ((pid = fork()) < 0)
  18.             err_sys("fork error");
  19.         else if (pid > 0)
  20.             exit(0);    /* parent from second fork == first child */
  21.         /*
  22.          * We're the second child; our parent becomes init as soon
  23.          * as our real parent calls exit() in the statement above.
  24.          * Here's where we'd continue executing, knowing that when
  25.          * we're done, init will reap our status.
  26.          */
  27.         sleep(2);
  28.         printf("second child, parent pid = %d\n", getppid());
  29.         exit(0);
  30.     }
  31.    
  32.     if (waitpid(pid, NULL, 0) != pid)  /* wait for first child */
  33.         err_sys("waitpid error");

  34.     /*
  35.      * We're the parent (the original process); we continue executing,
  36.      * knowing that we're not the parent of the second child.
  37.      */
  38.     exit(0);
  39. }
复制代码

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
5 [报告]
发表于 2008-10-24 17:23 |只看该作者
原帖由 无声无息 于 2008-10-24 17:06 发表
黑哥,你的环境测试结果是不是一样的?

我这里没有这种情况。
重新登录后,原来处于后台运行的ping没了。
启动后:
% ps -ef |grep ping
robert    3425  3416  0 17:31 pts/0    00:00:00 ping www.163.com
robert    3437  3416  0 17:32 pts/0    00:00:00 grep ping

退出,再重新登录后:
% ps -ef|grep ping
robert    3468  3455  0 17:36 pts/0    00:00:00 grep ping

[ 本帖最后由 blackold 于 2008-10-24 17:25 编辑 ]

论坛徽章:
0
6 [报告]
发表于 2008-10-24 17:25 |只看该作者
这时候你应该查看一下你的ping 进程的状态是啥..Zombie?

论坛徽章:
0
7 [报告]
发表于 2008-10-24 18:00 |只看该作者
会不会你的 huponexit 选项是关闭的,shell 退出时就不会发送SIGHUP到后台jobs, 用shopt 看看,
顺便要指出的是,要使huponexit生效,条件是
1. 打开这个选项,shopt -s huponexit   
2. login shell (如ssh登录的shell) 退出,才会发送 SIGHUP 至后台all jobs

[ 本帖最后由 seeLnd 于 2008-10-24 18:26 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2008-10-24 18:43 |只看该作者

回复 #6 nhw_cs 的帖子

额,都说了“Avoid zombie processes by calling fork twice”
对待zombie,ps命令会给出“Z”来标识...

论坛徽章:
0
9 [报告]
发表于 2008-10-24 18:48 |只看该作者

回复 #7 seeLnd 的帖子

经过试验,楼上分析的都对了

问题在于huponexit关闭了

shopt -s huponexit
shell 退出时才会会发送SIGHUP到后台jobs

$pstree
+-ping
+-sshd---sshd---sshd---bash---pstree

否则的话,向nhw_cs所说的那样子,ping就成为Zombie了,所以前面显示的一样,
bash进程终止后,init 进程会接管父进程留下的这些“孤儿进程”,所以PPID是1了

这个问题让我查资料,学习了,谢谢大家。

论坛徽章:
5
2015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:50:282015年亚洲杯之朝鲜
日期:2015-03-13 22:47:33IT运维版块每日发帖之星
日期:2016-01-09 06:20:00IT运维版块每周发帖之星
日期:2016-03-07 16:27:44
10 [报告]
发表于 2008-10-24 19:15 |只看该作者

回复 #9 无声无息 的帖子

查到什么资料了?记得发给你瞧瞧啊。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP