Find children of the process

36,339

Solution 1

You are looking for the pstree command. pstree by itself will list all the processes in a tree form (like lsblk does). You can use the -p flag to get the PIDs listed as well, and the -s to show parent process as well:

$ pstree -p 602
udisksd(602)-+-{cleanup}(607)
             |-{gdbus}(605)
             |-{gmain}(603)
             `-{probing-thread}(606)

A (probably) POSIX-compliant way of getting the child PIDs (that I'd mentioned in the comments elsewhere):

ps -o ppid= -o pid= -A | awk '$1 == <some pid>{print $2}'

This tells ps to write the parent PID and PID of all processes (without headings), and then uses awk to see which lines have the given PID in the first field (the parent PID), and prints the corresponding second field (the child PID).

Solution 2

If you just want to see the immediate children of a process whose PID is 123 you can use the ps command's --ppid option:

ps --ppid 123

You can combine that with the pidof command to get the children of a process by name i.e. given a process called foo

ps --ppid $(pidof foo)

Solution 3

Another option is, to use System Monitor (comes pre-installed). In SM Menubar mark "Dependencies" option, under "View", to have a visual feedback, showing parent and children process(es) like show in the screenshot below.

I prefer the CL (Command Line) myself and suggest, that those who use Linux, in this case Ubuntu on a daily basis, wisely invest their time in learning the basic commands, over GUI Applications or at least are able to master both to a certain degree!

enter image description here enter image description here

Solution 4

I'm not an expert, but reading the above answers it seemed to me that there is probably a more direct way to do this via the proc filesystem, e.g. for programmatic use in a script rather than human-readable display. And indeed there is: for a process with ID code $mypid, its child processes are listed in

/proc/$mypid/task/$mypid/children

e.g.

$ cat /proc/3123/task/3123/children 
3131 3133

Similarly, you can get the parent process ID via the "PPid" entry in the file

/proc/$mypid/task/$mypid/status

e.g.

$ grep PPid /proc/3131/task/3131/status
PPid:   3123
$ grep PPid /proc/3131/task/3131/status | cut -f2
3123

I'm not sure how portable this is beyond Linux systems, though.

Solution 5

if you want to see just the first-level children of a given parent process <pid> id look in /proc/<pid>/task/<tid>/children entry.

Note that this file contains the pids of first-level child processes. for the whole process tree, do it recursively.

this https://lwn.net/Articles/475688/ contains more information about it.

Share:
36,339

Related videos on Youtube

Mohammad Reza Rezwani
Author by

Mohammad Reza Rezwani

Msc of computer networks.

Updated on September 18, 2022

Comments

  • Mohammad Reza Rezwani
    Mohammad Reza Rezwani almost 2 years

    is there any way to know, who are children of the specific process ? for example those children which their parent ID is foo ?

  • muru
    muru almost 10 years
    OT: What theme is that?
  • v2r
    v2r almost 10 years
    I use Ubuntu + GnomeShell 3.2.1 The theme is called "AdwaitaDark" which is a GTK3 theme, but it is customized and not available, like you see it in the screenshots.
  • muru
    muru almost 10 years
    If portability is a concern: ps -o ppid= -o pid= -A | awk '$1 == <some pid>' | cut -f2 should be POSIX-compliant.