Find children of the process

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

5 Answers

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).

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)

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 hereenter image description here

2

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.

1

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 contains more information about it.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like