0

sry for not being familiar with question conventions since im new here.

i want to write a script that prints the full children's tree recursively of a given pid.

i got so far as:

ps h -ef | awk '$3='$input_pid' {print $2}' | sort -n

but this is just for printing the immediate children of the given pid (sorted).

the question is: how can i summon the recursive function for every child-pid of the given pid?

thanks...

N.R.
  • 3
  • 1

1 Answers1

1

try this

pstree -p parent_process_ID

If you want only pid to be displayed

pgrep -P parent_process_ID

or the simpler and a detailed display.

ps auxfww | grep -v grep | grep process_name
Gaurav Pundir
  • 1,496
  • 12
  • 14
  • the command "pgrep -P parent_process_ID" works only when i use it out of the script. for example, when i run it on the pid 2 it prints me all the processes running from 1 to 32656. why is that? – N.R. Feb 24 '16 at 06:58
  • Because pid 2 belongs to kthreadd process, which is kernel threads and you will see lots of children for kthreadd process. try this command too **ps -f -p 2 --ppid 2** for kthreadd process and its children. And pgrep works in the script too, I am not sure why it is not working with your script. try giving complete path of pgrep ie **/usr/bin/pgrep**, it should work. – Gaurav Pundir Feb 24 '16 at 07:10
  • i made a mistake. the command "pgrep -P parent_process_ID" works perfect. thanx!!! – N.R. Feb 24 '16 at 11:41