加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

linux – 如果子进程在后台启动并且父进程已经退出,如何知道父进

发布时间:2020-12-14 01:43:06 所属栏目:Linux 来源:网络整理
导读:我有一个在前台运行的子进程,他的父亲已经退出. proc / $pid / stat文件不再包含父pid如果已经退出父pid并且它显示1而不是原始父pid linux$cat /proc/6267/stat6267 (test3.sh) S 1 6265 ......# ^# |# I expected to get the origin parent pid but I get 1
我有一个在前台运行的子进程,他的父亲已经退出.

proc / $pid / stat文件不再包含父pid如果已经退出父pid并且它显示1而不是原始父pid

linux$cat /proc/6267/stat
6267 (test3.sh) S 1 6265 ......
#                 ^
#                 |
#        I expected to get the origin parent pid but I get 1

为了快速重现此行为,我们可以使用以下脚本

test2.sh

#!/bin/sh
echo "test2=$$"
./test3.sh &

test3.sh

#!/bin/sh
echo "test3=$$"
sleep 1000

执行:

linux$./test2.sh
test2=6318
test3=6319
linux$ps aux | grep test
 6319 root      1484 S    {test3.sh} /bin/sh ./test3.sh
linux$cat /proc/6319/stat
6319 (test3.sh) S 1 6318 2138 34816 6.......

解决方法

在(父)执行完毕后,我认为你不能知道进程的父级.但也许你可以运行另一个程序来监视你的原始程序并维护一个日志文件或其他东西.当然,init采用孤立的进程.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读