- 论坛徽章:
- 0
|
本帖最后由 wslmyb 于 2010-09-14 17:01 编辑
这个事情已经解决:(9月14日编辑)
参照http://linux.chinaunix.net/bbs/thread-1170852-1-1.html
中l.darkfire的回复说明。
这个都不清楚的话别说会用linux
清楚了的话也不敢说会linux,但是这个不清楚肯定不能说会。
我是不清楚,请高手帮忙。
纠结,崩溃,疑惑,无语
为了弄清楚:
linux中关于fork,source,exec,jobs,&,nohup,setsid
先共识一下基本概念:
fork:执行方式./asd.sh或sh asd.sh
source:执行方式. asd.sh或source asd.sh
关于这些概念的解释,参看网页,会的跳过。(baidu一下搜的n多个帖子贴的都是1篇,现在的帖子都这样,没办法,找几个比较有代表性的)
http://zhiyongz.javaeye.com/blog/593412
http://dolphin-ygj.javaeye.com/blog/455627
http://www.ibm.com/developerworks/cn/linux/l-cn-nohup/
http://baike.baidu.com/view/573513.htm
然后看我的实验,你能帮我把每个现象解释清楚就好了,主要问题是不清楚现象产生的原因。
实验环境:
[root@localhost abc]# cat /etc/issue
Red Hat Enterprise Linux Server release 5.4 (Tikanga)
[root@localhost abc]# cat /abc/101.sh
#!/bin/bash
while true
do
sleep 101
done
[root@localhost abc]# cat /abc/102.sh
#!/bin/bash
while true
do
sleep 102
done
以此类推,一直到106.sh(sleep 106)。
然后我开了7个伪终端,1-6分别运行101.sh-106.sh, 7用来运行ps等命令看结果。具体操作方法如下:
pts/1中:
[root@localhost /]# sh /abc/101.sh
pts/2中:
[root@localhost /]# source /abc/102.sh
pts/3中:
[root@localhost /]# sh /abc/103.sh &
pts/4中:
[root@localhost /]# source /abc/104.sh &
pts/5中:
[root@localhost /]# nohup /abc/105.sh &
pts/6中:
[root@localhost /]# setsid /abc/106.sh &
注意:现在各个终端都没有关闭,在pts/7中运行
(现象1)
ps auxf回车,看到结果:
root 6209 0.3 1.7 131936 18188 ? Sl 11:01 0:00 gnome-terminal
root 6213 0.0 0.0 2480 652 ? S 11:01 0:00 \_ gnome-pty-helper
root 6214 0.0 0.1 4912 1440 pts/1 Ss 11:01 0:00 \_ bash
root 6383 0.0 0.0 4728 992 pts/1 S+ 11:02 0:00 | \_ sh /abc/101.sh
root 6384 0.0 0.0 3956 476 pts/1 S+ 11:02 0:00 | \_ sleep 101
root 6250 0.0 0.1 4912 1440 pts/2 Ss 11:02 0:00 \_ bash
root 6385 0.0 0.0 3956 472 pts/2 S+ 11:02 0:00 | \_ sleep 102
root 6270 0.0 0.1 4912 1436 pts/3 Ss+ 11:02 0:00 \_ bash
root 6386 0.0 0.0 4728 992 pts/3 S 11:02 0:00 | \_ sh /abc/103.sh
root 6387 0.0 0.0 3956 472 pts/3 S 11:02 0:00 | \_ sleep 103
root 6290 0.0 0.1 4912 1440 pts/4 Ss+ 11:02 0:00 \_ bash
root 6388 0.0 0.0 4912 660 pts/4 S 11:02 0:00 | \_ bash
root 6389 0.0 0.0 3956 472 pts/4 S 11:02 0:00 | \_ sleep 104
root 6323 0.0 0.1 4912 1436 pts/5 Ss+ 11:02 0:00 \_ bash
root 6390 0.0 0.1 4728 1076 pts/5 S 11:03 0:00 | \_ /bin/bash /abc/105.sh
root 6391 0.0 0.0 3956 476 pts/5 S 11:03 0:00 | \_ sleep 105
root 6343 0.0 0.1 4912 1440 pts/6 Ss+ 11:02 0:00 \_ bash
root 6363 0.0 0.1 4912 1436 pts/7 Ss 11:02 0:00 \_ bash
root 6409 0.0 0.0 4608 928 pts/7 R+ 11:03 0:00 \_ ps auxf
root 6212 0.0 0.0 12304 964 ? S 11:01 0:00 scim-bridge
root 6393 0.0 0.0 4732 1000 ? Ss 11:03 0:00 /bin/bash /abc/106.sh
root 6394 0.0 0.0 3956 476 ? S 11:03 0:00 \_ sleep 106
(现象2)
ps -ef回车,看到结果:(为了看ppid)
pid ppid
root 6209 1 0 11:01 ? 00:00:00 gnome-terminal
root 6212 1 0 11:01 ? 00:00:00 scim-bridge
root 6213 6209 0 11:01 ? 00:00:00 gnome-pty-helper
root 6214 6209 0 11:01 pts/1 00:00:00 bash
root 6250 6209 0 11:02 pts/2 00:00:00 bash
root 6270 6209 0 11:02 pts/3 00:00:00 bash
root 6290 6209 0 11:02 pts/4 00:00:00 bash
root 6323 6209 0 11:02 pts/5 00:00:00 bash
root 6343 6209 0 11:02 pts/6 00:00:00 bash
root 6363 6209 0 11:02 pts/7 00:00:00 bash
root 6383 6214 0 11:02 pts/1 00:00:00 sh /abc/101.sh
root 6386 6270 0 11:02 pts/3 00:00:00 sh /abc/103.sh
root 6388 6290 0 11:02 pts/4 00:00:00 bash
root 6390 6323 0 11:03 pts/5 00:00:00 /bin/bash /abc/105.sh
root 6393 1 0 11:03 ? 00:00:00 /bin/bash /abc/106.sh
root 6410 6383 0 11:04 pts/1 00:00:00 sleep 101
root 6424 6250 0 11:04 pts/2 00:00:00 sleep 102
root 6425 6386 0 11:04 pts/3 00:00:00 sleep 103
root 6426 6388 0 11:04 pts/4 00:00:00 sleep 104
root 6429 6390 0 11:04 pts/5 00:00:00 sleep 105
root 6430 6393 0 11:05 ? 00:00:00 sleep 106
root 6431 6363 0 11:05 pts/7 00:00:00 ps -ef
注意:现在我关闭所有终端,然后再重新开一个终端,然后运行
(现象3)
ps axuf回车,看到结果:(发现101,102,103,104都消失了,因为关闭了所有终端,包括pts/1,pts/2,pts/3,pts/4)
root 6390 0.0 0.1 4728 1076 ? S 11:03 0:00 /bin/bash /abc/105.sh
root 6464 0.0 0.0 3956 472 ? S 11:06 0:00 \_ sleep 105
root 6393 0.0 0.0 4732 1000 ? Ss 11:03 0:00 /bin/bash /abc/106.sh
root 6465 0.0 0.0 3956 472 ? S 11:06 0:00 \_ sleep 106
root 6482 1.6 1.5 80684 16364 ? Sl 11:07 0:00 gnome-terminal
root 6486 0.0 0.0 2480 656 ? S 11:07 0:00 \_ gnome-pty-helper
root 6487 0.1 0.1 4912 1440 pts/1 Ss 11:07 0:00 \_ bash
root 6509 0.0 0.0 4608 916 pts/1 R+ 11:07 0:00 \_ ps auxf
root 6485 0.0 0.0 12304 952 ? S 11:07 0:00 scim-bridge
(现象4)
ps -ef回车,看到结果:(为了看ppid)
root 6390 1 0 11:03 ? 00:00:00 /bin/bash /abc/105.sh
root 6393 1 0 11:03 ? 00:00:00 /bin/bash /abc/106.sh
root 6465 6393 0 11:06 ? 00:00:00 sleep 106
root 6482 1 0 11:07 ? 00:00:00 gnome-terminal
root 6485 1 0 11:07 ? 00:00:00 scim-bridge
root 6486 6482 0 11:07 ? 00:00:00 gnome-pty-helper
root 6487 6482 0 11:07 pts/1 00:00:00 bash
root 6523 6390 0 11:08 ? 00:00:00 sleep 105
root 6524 6487 0 11:08 pts/1 00:00:00 ps -ef
我的一些观点或问题(和众帖子不一样的地方,也就是纠结的地方):
一 我认为所谓的“source模式是在当前进程”,这个话错了。通过现象2中104脚本部分很明白可以看到,明显不是一个进程(不同的pid),而是父子关系。
二 103 104都是用&执行的,所谓的放在后台,或者应该说在作业队列中。而一旦关上了终端,103和104就消失了。所以还要用nohup或setsid的方式。也就是说单纯的用&方式还是
要受到终端的影响。一般的像apache,mysql等进程,或xinet为守护进程的方式,不受终端影响。
三 和帖子一致的地方。nuhup在终端没有关闭时父进程为启动它的shell,关闭终端后父进程为1。setsid无论关闭终端与否,父进程都为1。
四 对于现象1的理解:
(1)101部分:明白。最简单的fork+前台运行。pts/1开了一个shell,执行sleep。
(2)102部分:明白。source+前台。按照帖子说的,sleep由pts/2本身运行。
(3)103部分:明白。fork+&(后台)。pts/3开了一个shell,只不过是放在了后台。
(4)104部分:不明白。source+&(后台)。请帮忙解释!
(5)105部分:明白。nuhup(没指定fork或source,应该为fork)+&。新开了一个进程,终端关闭前父进程为pts/5(现象1),关闭pts/5后父进程为1(现象3)。
(6)106部分:明白。setsid(没指定fork或source,应该为fork)+&。新开了一个进程,父进程始终为1(现象1,3)。
说到底最不明白的是2点
1 source是否新开了一个进程。
没开的说法:看现象1中的102部分
开了的说法:看现象1中的104部分
也许这个问题应该结合是否为前台后台等综合讨论。
2 所谓的&是否受终端的影响?终端一旦关闭则&的都将关闭(我做的实验都是这样,除非特殊的脚本能做成守护进程的方式)。主要现在网络上的帖子都是讲的fg,bg,jobs怎么用
,没有考虑到终端问题。
补充说明:
一 source问题:
请参看现象2中的sleep 104部分。
root 6209 1 0 11:01 ? 00:00:00 gnome-terminal //图形界面,父进程为1,没有问题
root 6290 6209 0 11:02 pts/4 00:00:00 bash //本身的pts/4,父进程为图形界面,没有问题
root 6388 6290 0 11:02 pts/4 00:00:00 bash //运行sleep 104命令的shell,父进程为6290
root 6426 6388 0 11:04 pts/4 00:00:00 sleep 104 //sleep 104命令,父进程shell为6388
主要是6388这个进程是怎么来的问题
二 后台&问题:
现有脚本first.sh和second.sh
[root@localhost /]# cat /abc/first.sh
#!/bin/bash
source /abc/second.sh &
[root@localhost /]# cat /abc/second.sh
#!/bin/bash
while true
do
sleep 500
done
现在执行
[root@localhost /]# source /abc/first.sh &
[1] 10458
[root@localhost /]# jobs
[1]+ Done source /abc/first.sh
[root@localhost /]#
ps auxf回车:
root 10386 0.0 1.6 81644 17388 ? Sl 14:24 0:00 gnome-terminal
root 10391 0.0 0.0 2480 656 ? S 14:24 0:00 \_ gnome-pty-h
root 10392 0.0 0.1 4912 1556 pts/1 Ss 14:24 0:00 \_ bash
root 10461 0.0 0.0 4608 920 pts/1 R+ 14:27 0:00 \_ ps auxf
root 10390 0.0 0.0 12304 952 ? S 14:24 0:00 scim-bridge
root 10459 0.0 0.0 4912 696 pts/1 S 14:27 0:00 bash
root 10460 0.0 0.0 3956 472 pts/1 S 14:27 0:00 \_ sleep 500
发现10459这个bash的父进程已经是根了。
然后退出这个终端,重新开一个终端
ps auxf回车:
root 10459 0.0 0.0 4912 696 ? S 14:27 0:00 bash
root 10460 0.0 0.0 3956 472 ? S 14:27 0:00 \_ sleep 500
root 10492 3.2 1.6 81528 17256 ? Sl 14:28 0:00 gnome-terminal
root 10496 0.0 0.0 2480 656 ? S 14:28 0:00 \_ gnome-pty-h
root 10497 0.3 0.1 4912 1496 pts/2 Ss 14:28 0:00 \_ bash
root 10518 0.0 0.0 4608 920 pts/2 R+ 14:28 0:00 \_ ps auxf
root 10495 0.0 0.0 12304 952 ? S 14:28 0:00 scim-bridge
发现10459这个进程还在。
有的人说&放在后台会受终端的影响,可是现在这个10459和10460进程都在,怎么解释? |
|