LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   shell scripting (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-862799/)

deeppal123 02-15-2011 05:09 AM

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

EricTRA 02-15-2011 05:12 AM

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

deeppal123 02-15-2011 05:20 AM

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

EricTRA 02-15-2011 05:32 AM

Hello,

OK, I'll take your word for it :)

Did you have a look at the man page of ps?
Code:

man ps
A quote from that same man page:
Quote:

To see every process with a user-defined format:
ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm
ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
ps -eopid,tt,user,fname,tmout,f,wchan
If you take the middle one, substitute euid with user and pipe it into grep -v to exclude root like this:
Code:

ps axo stat,user,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm | grep -v root
you'll get this as output:
Code:

STAT USER      RUID TT      TPGID  SESS  PGRP  PPID  PID %CPU COMMAND
Ss  rpc        32 ?          -1  2399  2399    1  2399  0.0 portmap
Ss  dbus        81 ?          -1  2471  2471    1  2471  0.0 dbus-daemon
Ss  68          68 ?          -1  2567  2567    1  2567  0.0 hald
S    68          68 ?          -1  2567  2567  2568  2576  0.0 hald-addon-acpi
S    68          68 ?          -1  2567  2567  2568  2582  0.0 hald-addon-keyb
SLs  ntp        38 ?          -1  2697  2697    1  2697  0.0 ntpd
Ss  smmsp      51 ?          -1  2723  2723    1  2723  0.0 sendmail
Ss  xfs        43 ?          -1  2771  2771    1  2771  0.0 xfs
Ss  avahi      70 ?          -1  2818  2818    1  2818  0.0 avahi-daemon
Ss  avahi      70 ?          -1  2819  2819  2818  2819  0.0 avahi-daemon
Sl  luci      101 ?          -1  1972  2874    1  2884  0.0 python
Ss  luci      101 ?          -1  2940  2940    1  2940  0.0 stunnel
Ss  gdm        42 ?          -1  3082  3082  3064  3082  0.0 gdmgreeter

I'll let you figure out what options you want to suppress from the command.

Kind regards,

Eric

deeppal123 02-15-2011 11:13 PM

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.

chrism01 02-16-2011 12:09 AM

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/

EricTRA 02-16-2011 01:19 AM

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
That will give you output like this example from my laptop:
Code:


USER      PPID  PID COMMAND
daemon      1  1444 portmap
daemon      1  1767 atd
103          1  1820 dbus-daemon
avahi        1  1921 avahi-daemon
avahi    1921  1922 avahi-daemon
109          1  2030 hald
109      2031  2077 hald-addon-acpi
mysql    2110  2239 mysqld
108          1  2527 polkit-gnome-au
eric        1  2608 gnome-keyring-d
eric      2533  2627 x-session-manag
eric      2627  2660 ssh-agent
eric        1  2663 dbus-launch
eric        1  2664 dbus-daemon

Now, you're talking about a script. What exactly do you want to do with it? Do you want to list all child processes of a particular ppid using that script? Or just sort the list of commands depending on their parent process ID? A bit more detail of what you exactly want will help out a lot. Also it would be great if you posted already what you have for your script and not only what command(s) you use or are thinking about.

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

ashwin_cse 02-16-2011 03:20 AM

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>

deeppal123 02-16-2011 11:13 PM

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.

Tinkster 02-16-2011 11:37 PM

The whole scripting approach to me looks like overkill :}

Code:

pgrep -l -P $( pgrep kthread )
Wrap that into a function and you're away laughing!



Cheers,
Tink

deeppal123 02-17-2011 02:51 AM

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.