LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-15-2011, 06:09 AM   #1
deeppal123
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Rep: Reputation: 0
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
 
Old 02-15-2011, 06:12 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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
 
Old 02-15-2011, 06:20 AM   #3
deeppal123
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
Old 02-15-2011, 06:32 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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
 
Old 02-16-2011, 12:13 AM   #5
deeppal123
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 02-16-2011, 01:09 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
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/
 
Old 02-16-2011, 02:19 AM   #7
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
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

Last edited by EricTRA; 02-16-2011 at 02:20 AM.
 
Old 02-16-2011, 04:20 AM   #8
ashwin_cse
Member
 
Registered: Jul 2004
Distribution: arch, rhel
Posts: 133

Rep: Reputation: 22
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>
 
Old 02-17-2011, 12:13 AM   #9
deeppal123
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Original Poster
Rep: Reputation: 0
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.
 
Old 02-17-2011, 12:37 AM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 02-17-2011, 03:51 AM   #11
deeppal123
LQ Newbie
 
Registered: Feb 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Thumbs up 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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Terminal functions for shell scripting with Shell Curses LXer Syndicated Linux News 0 03-27-2008 12:50 AM
SHELL scripting/ shell functions mayaabboud Linux - Newbie 6 12-26-2007 09:18 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 04:32 PM


All times are GMT -5. The time now is 04:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration