shell scripting
well i have just started with shell scripting...
i need help regarding a script how to find all child processes of a parent process given to script as argument. i would like to know the methodology as well as approach. thanks in advance |
Hello and Welcome to LinuxQuestions,
This sounds like homework to me. LQ users are not in the habit of doing the legwork for you, so you'll have to demonstrate what you already have put together and where it's failing or where errors arise. Then we'll try to help you out. Kind regards, Eric |
i am no longer in school....
anyways i was trying with ps but it did not help me.. i was trying to do something like this ps -o user,pid,ppid,command -ax|grep -v root but didnt work for me |
Hello,
OK, I'll take your word for it :) Did you have a look at the man page of ps? Code:
man ps Quote:
Code:
ps axo stat,user,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm | grep -v root Code:
STAT USER RUID TT TPGID SESS PGRP PPID PID %CPU COMMAND Kind regards, Eric |
basically i have tried these things,grep -v root,but it just shows me the fields,
it has to be a shell script so i just was thinkn whether i should do 2 ps first to get the ppid and then to get the pid,but i am losing track after this. i hv just started,so any help would be of great help. thanks a lot. |
If you are just starting shell scripting I highly recommend these:
http://rute.2038bug.com/index.html.gz http://tldp.org/LDP/Bash-Beginners-G...tml/index.html http://www.tldp.org/LDP/abs/html/ |
Hello,
It all bottles down to what you really need, if you only need user, ppid, pid and command for any user except root, then this will do: Code:
ps axo user,ppid,pid,comm |grep -v root Code:
And do take a look at the links chrism01 pointed you too, they're pretty good when you're starting with Bash scripting. Kind regards, Eric |
have you tried ps --ppid <parent process id>
have you tried ps --ppid <parent process id>. Get the parent process id in your script. Then echo the output of ps --ppid < parent pid>
|
temp1=$( ps -ef | grep $1 | awk 'NR==1 {print $2}') # this gets the pid of the service
echo $temp1 temp2=$(ps -ef | wc -l) # no of iterations echo $temp2 for(( i = 2 ; i <= $temp2 ; i++ )) do temp3=$(ps -ef | awk NR==$i'{ print $3 }') # picking up the ppid of processes # echo temp3 == $temp3 # echo temp2 == $temp2 if [ $temp1 -eq $temp3 ] ; then temp4=$(ps -ef | awk NR==$i' {print $8}') # if there is a match cut the field containing name echo "pid : $temp1" echo "ppid : $temp3" echo $temp4 fi done this is what i have done..basically a process is given as cmdline argument...and my script tries to find the names of all the children of that process...only prob with this script when i tested with kthreadd as the parent process was it showed 29 of the child processes instead of 41 which it should have shown. |
The whole scripting approach to me looks like overkill :}
Code:
pgrep -l -P $( pgrep kthread ) Cheers, Tink |
hail tinker
^^^ that was just awesome!!!! its really wonderful to know things and that too when it is so special!!! thanks a lot.kudos.
however ,i would like to know y my script was showing only 29 of the possible 41 children. |
All times are GMT -5. The time now is 03:08 AM. |