0

I am trying to generate a list of PID's to pass to strace I only want the PID's php-fpm. I can get the correct output by using grep with awk but am looking for an awk only answer. Here is what I have done.

This gives the desired output:

$ ps ajxf |grep '[p]hp-fpm: pool' |awk '{print $2}'
21547
21548
21549
21938
31131
31132
31133
31189
36283
10549
12179
12893
12964
12989
13565
14420
14421
15848
17737

This one did not:

$ ps ajxf |awk '/php-fpm: pool/{if (NR!=1) {print $2}}'
22903  <--- This is the PID of the awk
21547
21548
21549
21938
31131
31132
31133
31189
36283
10549
12179
20328
20392
20393
20618
22006
22793
22794
22804
Giacomo1968
  • 3,542
  • 27
  • 38
menders65
  • 25
  • 6
  • You can use square brackets in the AWK version in the same way that you used them in the `grep` version. Also, you don't need to use `if`: `awk '/php-fpm: pool/ && NR != 1 {print $2}'`, but `pidof` is better. – Dennis Williamson Apr 13 '14 at 19:28

1 Answers1

3

I think you might find pidof much easier to use.

pidof php
24293 19810 19467

You can use ~ to match a field and ^ to anchor the match to the beginning of the field.

awk '$6 ~ /^something/ {print $2}'
user9517
  • 115,471
  • 20
  • 215
  • 297
  • That also returns the php-fpm master process which I do not want – menders65 Apr 13 '14 at 13:38
  • your second answer did the trick. Both of these work what is it that the carrot does exactly `ps ajxf |awk '$12 ~ /^pool/{print $2,$13}'` `ps ajxf |awk '$12 ~ /pool/{print $2,$13}'` – menders65 Apr 13 '14 at 14:15