How to get parent pid from a given children pid?

I know I can mannully check it under /proc, I am wonder if there is a smart/better way to achieve this in Ubuntu. Note the parent may or may not be killed.

Thanks

------------------------------------------------------------------------------------------------

Using pstree by command name

Using pstree you can search by the child process name and get the Process ID (PID) along with the parents, grandparents and any children of the child process:

$ pstree -hp | grep sleep
|-cron(763)---cron(795)---sh(839)---display-auto-br(841)---sleep(8414)

In this case sleep is the child command and it's PID is 8414. It's parent ID is 841 and is called display-auto-brightness. The grandparent is a shell (sh) with a process ID of 839. The great-grandparent is cron with a process ID of 795. The great-great-grandparent is also cron with a process ID of 763.

If you want to search by Process ID of sleep instead of name you can use:

$ pstree -hp | grep 14653
|-cron(763)---cron(795)---sh(839)---display-auto-br(841)---sleep(14653)

Notice the sleep process ID changed to 14653. The parent (PID 841) sleeps for 1 minute, wakes up for a split second and then starts a new sleep command which gets a new process ID. This is another reason why searching for sleep is easier than searching by process ID.

This code was taken from: Automatically adjust display brightness based on sunrise and sunset and adapted to this question.


To see a nested chain all the way back to boot process use the PID instead of name:

$ pstree -aps 8541
systemd,1 splash fastboot kaslr
└─cron,763 -f
└─cron,795 -f
└─sh,839 -c /usr/local/bin/display-auto-brightness
└─display-auto-br,841 /usr/local/bin/display-auto-brightness
└─sleep,8541 60

Note: Another minute has passed and the sleep command gets a new PID (8541).

查看进程id, 父进程id _How do I get the parent process ID of a given child process?的更多相关文章

  1. 获取进程ID,父进程ID,进程完整路径

    准备写一个进程管理的功能模块,今天下午先写了扫描获取本机各个进程路径,获取各个进程映像名称,进程完整路径. 要获取进程信息,第一步想到的就是提权,提权代码用过多次了,今天也小结了一下(http://w ...

  2. PHP多进程学习(二)__fork起多个子进程,父进程的阻塞与非阻塞

    先简单来了解一下多进程 [来初步了解一下PHP多进程及简单demo] php的多进程是不是可以无限制的fork子进程?fork调用的一个奇妙之处就是它仅仅被调用一次,却能够返回两次,它可能有三种不同的 ...

  3. 【LINUX】主进程、父进程、子进程、守护进程的概念

    一.摘要 详解父进程.子进程.守护进程的区别,例子稍候补充 二.定义区别 主进程 程序执行的入口,可以理解为常用的main 函数 父进程 对于子进程而言, 子进程的创造者,可有多个子进程. 任何进程都 ...

  4. [Win32]获取指定进程的父进程PID

    // // #include <Windows.h> #include <winnt.h> #include <winternl.h> typedef NTSTAT ...

  5. 进程的基本属性:进程ID、父进程ID、进程组ID、会话和控制终端

    摘要:本文主要介绍进程的基本属性,基本属性包含:进程ID.父进程ID.进程组ID.会话和控制终端. 进程基本属性 1.进程ID(PID) 函数定义:      #include <sys/typ ...

  6. 二十一、Linux 进程与信号---进程查看和进程状态、进程调度和进程状态变化、进程标识

    21.1 进程查看和进程状态 21.1.1 ps 指令 ps 指令通常可以查看到进程的 ID.进程的用户 ID.进程状态和进程的 Command ps:查看当前用户启动的进程 ps -ef:详细查看后 ...

  7. 关于父进程和子进程的关系(UAC 绕过思路)

    表面上看.在windows中. 假设是a进程创建了b进程,那么a进程就是b进程的父进程.反之,假设是b创建了a,那么b进程就是a的父进程,这是在windows出现以来一直是程序员们都证实的,可是在在w ...

  8. [并发编程 - socketserver模块实现并发、[进程查看父子进程pid、僵尸进程、孤儿进程、守护进程、互斥锁、队列、生产者消费者模型]

    [并发编程 - socketserver模块实现并发.[进程查看父子进程pid.僵尸进程.孤儿进程.守护进程.互斥锁.队列.生产者消费者模型] socketserver模块实现并发 基于tcp的套接字 ...

  9. 【zombie】如何查看并杀死僵尸进程?

    [zombie]如何查看并杀死僵尸进程? 赏金Micheal关注 2019.03.31 19:40:15字数 1,016阅读 4,373 僵尸进程定义 In UNIX System terminolo ...

随机推荐

  1. bp神经网络模型推导与c语言实现(转载)

    转载出处:http://www.cnblogs.com/jzhlin/archive/2012/07/28/bp.html BP 神经网络中的 BP 为 Back  Propagation 的简写,最 ...

  2. Django REST Framework的序列化器是什么?

    # 转载请留言联系 用Django开发RESTful风格的API存在着很多重复的步骤.详细可见:https://www.cnblogs.com/chichung/p/9933861.html 过程往往 ...

  3. HTML布局相关的CSS样式属性

    # 转载请留言联系 注意,样式属性是写进CSS里面的. 布局常用样式属性: width 设置元素(标签)的宽度,如:width:100px; height 设置元素(标签)的高度,如:height:2 ...

  4. import module与from module import * 两种模块导入有何区别

    #原创,转载请留言联系 区别1 import module 引用共享变量时,要使用module.变量名. 而from module import * 直接使用变量名即可 区别2(有点绕) import ...

  5. Delphi 中 函数参数中的 const 修饰符的本质以及注意事项

    来自:http://blog.csdn.net/farrellcn/article/details/9096787 ------------------------------------------ ...

  6. KVM(五)libvirt 介绍

    1. Libvirt 是什么 为什么需要Libvirt? Hypervisor 比如 qemu-kvm 的命令行虚拟机管理工具参数众多,难于使用. Hypervisor 种类众多,没有统一的编程接口来 ...

  7. css项目列表如何水平放置

    列表项目默认分行排列,那么将列表项设置浮动就可以实现水平放置 1 li{float:left;} 示例如下: 创建Html元素 1 2 3 4 5 6 <ul>     <li> ...

  8. beijing2016

    4625: [BeiJing2016]水晶 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 201  Solved: 70[Submit][Status ...

  9. 使用Redis实现高并发分布式序列号生成服务

    序列号的构成 为建立良好的数据治理方案,作数据掌握.分析.统计.商业智能等用途,业务数据的编码制定通常都会遵循一定的规则,一般来讲,都会有自己的编码规则和自增序列构成.比如我们常见的身份证号.银行卡号 ...

  10. PHP的文件操作类

    <?php class file { function file() { die("Class file can not instantiated!"); } //创建目录 ...