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. git error: unable to write file xxx,git fatal: unable to write new index file

    执行git checkout -- . error: unable to write file mobile/manifest.jsonfatal: unable to write new index ...

  2. nginx、apache、tomcat的区别

    1.Apache Apache支持模块化,性能稳定,Apache本身是静态解析,适合静态HTML.图片等,但可以通过扩展脚本.模块等支持动态页面等. 缺点:配置相对复杂,自身不支持动态页面. 2.To ...

  3. [ Python - 12 ] 线程的信号量、标志位及队列

    线程的信号量 线程的信号量是同时允许一定数量的线程更改数据,主要作用在于限制线程的并发. #!_*_coding:utf-8_*_ # Author: hkey import threading, t ...

  4. linux下redis的最佳实践(Master-Slave)

    本文演示了redis在同一台linux上的安装及运行多个实例,并演示了主从复制,以及如何进行主从的切换. 1. 下载 $ wget http://download.redis.io/releases/ ...

  5. WIN7下使用sublime text3替代arduino IDE(安装方法和所遇到的问题)

    用了一段时间Arduino IDE,感觉比较简陋~~很多功能都没有~虽然不影响使用啦~(主要是启动速度有点慢...我的破笔记本….), 网上搜寻了下,发现sublime text有插件可以替代,这就比 ...

  6. zookeeper与activemq整合

    (1)zookeeper与activemq原理 使用ZooKeeper实现的Master-Slave实现方式,是对ActiveMQ进行高可用的一种有效的解决方案,高可用的原理:使用ZooKeeper( ...

  7. Response 部分功能

    设置状态码的方法:    void setStatus(int sc)     void setStatus(int sc, String sm) 设置响应头的方法:    void setHeade ...

  8. js中OOP小指南

    js中OOP小指南 在指南中,我将尝试解析以面向对象规范聚焦的es6的新特性. 首先, 什么是设计模式 范例是某个事务的例子或模型,在某种情况下,按照一种模式创建了计算机程序. 什么是面向对象 显然你 ...

  9. ubuntu下执行ulimit返回“不允许的操作”,问题解决思路

    在ubuntu下执行ulimit,希望修改允许的最大打开文件数,但返回“不允许的操作”. 使用ulimit -a查看当前配置 core file size (blocks, -c) 0 data se ...

  10. [php] 解析JSON字符串

    $json_str = '{"thumb":"","photo":[{"url":"557a3d2184db0 ...