2

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.

2

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.

0

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.