Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to find ppid processes children with ps

Status
Not open for further replies.

warburp

IS-IT--Management
Oct 27, 2003
44
GB
Hi

I'm trying to write a script to display all processes with a ppid that I am holding in an array. Can anyone suggest what ps command should be used. My current code is below:

while (( i < ${NUMPIDS} )) #while the array count < i
do
if [ i -eq 0 ] #set the header with pid 0
then
echo &quot;\t \t`ps -p 999999999999999999 -o pid,user,time,args`\n&quot; # set up header
((i+=1))
fi

echo &quot;\t $i -- \t`ps -p ${pidarray} -o pid,user,time,args|tail -1`\n&quot;
((i+=1))

done

Thanks
 
Try :

set -A pidarray 54562 56588 52880 54326 # for example

ps -p &quot;${pidarray[*]}&quot; -o pid,user,time,args|awk '{print &quot;\t&quot;$0}'


Jean Pierre.
 
Thanks Jean Pierre, but I don't think I have explained it correctly.
I have a pid in an array and I want to find the pids of all other processes with a parent pid (ppid) = pid in the array (i.e. find all the child processes)

Thanks
 
Try something like this:
Code:
echo &quot;\t\t`ps -p 99999999 -o pid,user,time,args`\n&quot;
i=1
while (( i < ${NUMPIDS} )); do
 ppid=${pidarray[i]}
 echo &quot;\t $i --\t`ps -p $ppid -o pid,user,time,args|tail -1`&quot;
 ps -e -o ppid,pid,user,time,args | awk '
  $1=='$ppid'{sub(&quot;[1-9][0-9]*&quot;,&quot;\t\t&quot;);print}'
  echo
 ((i+=1))
done

Hope This Help
PH.
 
The following code print all processes with their parent pid in the array &quot;pidarray&quot; (don't need pid 0 for header) :

set -A pidarray 1 2630

ps -e -o ppid,pid,user,time,args | awk -v Ppids=&quot;${pidarray[*]}&quot; '
function PrintProcess( ,infos) {
gsub(&quot;^ *[^ ]+&quot;,&quot;&quot;) ;
print &quot;\t&quot; $0 ;
}
BEGIN {
gsub(&quot;^ *| *$&quot;,&quot;&quot;, Ppids) ;
gsub(&quot; +&quot;, &quot;|&quot;, Ppids) ;
pattern = &quot;^ *(&quot; Ppids &quot;)$&quot; ;
}
NR==1 {
PrintProcess() ;
next ;
}
$1 ~ pattern {
PrintProcess() ;
}
'


Jean Pierre.
 
A better way to find child processes is with [tt]ptree[/tt]. This command will show all processes in a process tree, including parent and all children.
Code:
    ptree $PID
Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top