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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
11-25-2016, 10:59 AM
|
#1
|
LQ Newbie
Registered: Nov 2016
Posts: 7
Rep: 
|
Question about awk
Hi,
I am printing the children of a process using this command:
Code:
ps -aef | grep 13783 | grep -v grep | grep -v ps | grep -v $0
and it shows the children correctly. (2 in my case)
The thing is that when I pipe it to awk to get the PIDs like this:
Code:
ps -aef | grep 13783 | grep -v grep | grep -v ps | grep -v $0 | awk '{print $2}'
I get 3 numbers. I don't know where that 3rd number is coming from.
|
|
|
11-25-2016, 11:47 AM
|
#2
|
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,908
|
You might check that "grep 13783" looks for the string 13783 - and that may be a substring of another process id.
I did a "ps -aef | grep $$ | grep -v grep | grep -v ps" to see what this looked like - and got two processes, one mine (1245), and one from another user (11245).
You can prevent that by doing better pattern search: grep " $pid " as the spaces will reduce that issue. It doesn't prevent the resulting string from being in some other field though.
To do that would require more of the effort to be put into the awk script. Let it do the search for the parent pid ( third field), as well as being able to remove the cmd 'ps' from the selected list.
Last edited by jpollard; 11-25-2016 at 11:51 AM.
|
|
|
11-25-2016, 11:53 AM
|
#3
|
LQ Newbie
Registered: Nov 2016
Posts: 7
Original Poster
Rep: 
|
if i just do:
Code:
ps-aef | grep 13783
I get:
Code:
cs11amb 13783 13324 0 08:44 pts/0 00:00:00 bash
cs11amb 13971 13783 0 08:44 pts/0 00:00:00 vim ff
cs11amb 14000 13783 0 08:44 pts/0 00:00:00 vim ss
cs11amb 19461 13783 7 09:49 pts/0 00:00:00 ps -aef
cs11amb 19463 13783 0 09:49 pts/0 00:00:00 grep 13783
when i grep out the extras, I only get
Code:
cs11amb 13971 13783 0 08:44 pts/0 00:00:00 vim ff
cs11amb 14000 13783 0 08:44 pts/0 00:00:00 vim ss
which are the correct children and what I want.
The problem is when I use awk, it prints:
|
|
|
11-25-2016, 12:02 PM
|
#4
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
Is that last PID that of the "awk" process itself?
Also, with "awk" there you don't need any of the "grep" processes at all.
|
|
1 members found this post helpful.
|
11-25-2016, 12:06 PM
|
#5
|
LQ Newbie
Registered: Nov 2016
Posts: 7
Original Poster
Rep: 
|
Quote:
Originally Posted by Turbocapitalist
Is that last PID that of the "awk" process itself?
Also, with "awk" there you don't need any of the "grep" processes at all.
|
I swear to god I tried to grep out awk before but it still showed up.
It is working now. Thank youuu.
Could you be more specific when you say I don't need any of the greps?
|
|
|
11-25-2016, 12:13 PM
|
#6
|
LQ Newbie
Registered: Nov 2016
Posts: 7
Original Poster
Rep: 
|
In a script, I am trying to put the children in an array by saying:
Code:
for child in `ps -aef | grep $pid | grep -v grep | grep -v ps | grep -v $0 | grep -v awk | grep -v bash | awk '{print $2}'`; do
arrayOfChildren+=("$child")
done
but echoing the array afterward
instead of the children PIDs as the array contents I get:
5
5
If I do
Code:
echo `ps -p ${#arrayOfChildren[0]}`
echo `ps -p ${#arrayOfChildren[1]}`
I get
PID TTY TIME CMD 5 ? 00:00:00 stopper/0
PID TTY TIME CMD 5 ? 00:00:00 stopper/0
Last edited by backslashV; 11-25-2016 at 01:21 PM.
|
|
|
11-25-2016, 12:47 PM
|
#7
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,664
|
why don't you try pgrep -P ?
|
|
|
11-25-2016, 12:48 PM
|
#8
|
LQ Newbie
Registered: Nov 2016
Posts: 7
Original Poster
Rep: 
|
Quote:
why don't you try pgrep -P ?
|
I am not allowed.
|
|
|
11-25-2016, 01:16 PM
|
#9
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,664
|
You will not be able to list all the children "in one", because all the members of your chain (grep, awk, whatever) will be children and you need to exclude them, but actually there can be other grep/awk/whatever children which should be reported but will be ignored too. The best you can do is to save the output of the command ps and process the result afterward, or use a single awk which will exclude itself and print everything else.
ps -aef | awk -vmypid=$$ { look for $3 as parent pid and also look for aw }
why aren't you allowed to use pgrep?
|
|
|
11-25-2016, 01:23 PM
|
#10
|
LQ Newbie
Registered: Nov 2016
Posts: 7
Original Poster
Rep: 
|
Quote:
Originally Posted by pan64
You will not be able to list all the children "in one", because all the members of your chain (grep, awk, whatever) will be children and you need to exclude them, but actually there can be other grep/awk/whatever children which should be reported but will be ignored too. The best you can do is to save the output of the command ps and process the result afterward, or use a single awk which will exclude itself and print everything else.
ps -aef | awk -vmypid=$$ { look for $3 as parent pid and also look for aw }
why aren't you allowed to use pgrep?
|
I want to store the result of ps in an array.
Code:
for child in `ps -aef | grep $pid | grep -v grep | grep -v ps | grep -v $0 | grep -v awk | grep -v bash | awk '{print $2}'`; do
arrayOfChildren+=("$child")
done
echoing the contents with ps shows:
Code:
PID TTY TIME CMD 5 ? 00:00:00 stopper/0
PID TTY TIME CMD 5 ? 00:00:00 stopper/0
so basically the contents of the array are:
5
5
but before putting into the array I echo the output of
Code:
ps -aef | grep $pid | grep -v grep | grep -v ps | grep -v $0 | grep -v awk | grep -v bash | awk '{print $2}'
and i get the correct children PIDs like this:
13432 13324
I am not sure why I am not allowed to use that. I just asked and the TA said no.
|
|
|
11-25-2016, 02:35 PM
|
#11
|
LQ Newbie
Registered: Nov 2016
Posts: 7
Original Poster
Rep: 
|
Solved
It appears that the problem was putting a # before the array name: ${#arrayOfChildren[0]}
removing that solved the problem.
Thanks everyone.
|
|
|
11-25-2016, 02:54 PM
|
#12
|
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,042
|
Also, here is your possible all awk solution to feed your loop:
Code:
ps -aef | awk -vpid=$pid '$3 == pid && $NF != "-aef" && $NF !~ "pid"'
|
|
1 members found this post helpful.
|
All times are GMT -5. The time now is 03:28 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|