Get all child processes of a process

is there a universal way of getting the children of a particular process? i'm looking for a solution that works across different OSes...linux, aix, sunos, hpux.

i did a search online and i kept finding answers that were specific to Linux..i.e. pstree.

i want to be able to specify a process ID and then have the command spit out all its children processes. is this possible?

Check the man page for the ps utility -o option. The names used for various fields in the output may vary from system to system, but there should be heading like pid , ppid , and command OR PID , PPID , and CMD . Using those options, you can use something like (using a BSD-based ps as an example):

ps -A -o 'ppid pid command'

to get a list of all processes on your system showing its parent process ID, its process ID, and its command name and arguments. And to find all of the children of a given process ID, you could use:

ps -A -o 'pid pid command' | grep '^ *pid '

where pid is the process ID of the process whose children you want to find.

If you want to find all of a process' un-orphaned descendants you could use an awk script instead of grep to create a tree of children, grandchildren, great-grandchildren, ... for all processes with a given process ID as a parent recursively. (Unfortunately, any process that has been orphaned, will have PPID 1 with no way to tie it back to its birth-parents.)

1 Like

I usually have taken -e not -A for portable ps.
HP-UX needs environment variable UNIX95 to take the -o option.
If your pid is 4711 then a portable command to find its children is

UNIX95=1 ps -e -o ppid= -o args= | awk '$1==ppid' ppid=4711 
1 Like