Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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:
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.
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.
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.
^^^ 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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.