4

I'm trying to get a name of a process given its process id along with its children, parent and grandparent names.

I've used ps -eo for getting the name of the process id, but not not for any of its children, parents or grandparents.

3 Answers 3

3

Try this:

pstree -pls your_pid

pstree - display a tree of processes.

  • -p Show PIDs. PIDs are shown as decimal numbers in parentheses after each process name.

  • -s Show parent processes of the specified process.

  • -l Display long lines. By default, lines are truncated to either the COLUMNS environment variable or the display width.

1

To get a process name, given its pid:

pid=42
ps -o comm= -p $pid

To get the names of the child processes of a given pid (Linux procps):

ps -o comm= --ppid $pid

To get the names of the child processes of a given pid (POSIX):

for cpid in $(ps -eo pid=,ppid= | awk -v pid=$pid '$2==pid{print $1}'); do ps -o comm= -p $cpid; done

To get the name of the parent process of a given pid:

ps -o comm= -p $(ps -o ppid= -p $pid)

To get the name of the grandparent process of a given pid (assuming you haven't hit your init system yet):

ps -o comm= -p $(ps -o ppid= -p $(ps -o ppid= -p $pid))
0

You can get a list of child process IDs using the following command:

pgrep -P $parent_pid

Credit to this answer.

1
  • This does not seem to do what the user asks for. which is to get the parent process, the child process(es) and grandchildren process(es) of a given process.
    – Kusalananda
    Aug 7, 2019 at 9:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .