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 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}' |
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. |
if i just do:
Code:
ps-aef | grep 13783 Code:
cs11amb 13783 13324 0 08:44 pts/0 00:00:00 bash Code:
cs11amb 13971 13783 0 08:44 pts/0 00:00:00 vim ff The problem is when I use awk, it prints: Code:
13971 |
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. |
Quote:
It is working now. Thank youuu. Could you be more specific when you say I don't need any of the greps? |
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 instead of the children PIDs as the array contents I get: 5 5 If I do Code:
echo `ps -p ${#arrayOfChildren[0]}` PID TTY TIME CMD 5 ? 00:00:00 stopper/0 PID TTY TIME CMD 5 ? 00:00:00 stopper/0 |
why don't you try pgrep -P ?
|
Quote:
|
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? |
Quote:
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 Code:
PID TTY TIME CMD 5 ? 00:00:00 stopper/0 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}' 13432 13324 I am not sure why I am not allowed to use that. I just asked and the TA said no. |
Solved
It appears that the problem was putting a # before the array name: ${#arrayOfChildren[0]}
removing that solved the problem. Thanks everyone. |
Also, here is your possible all awk solution to feed your loop:
Code:
ps -aef | awk -vpid=$pid '$3 == pid && $NF != "-aef" && $NF !~ "pid"' |
All times are GMT -5. The time now is 07:54 PM. |