Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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
share|improve this question
    
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 at 19:28

1 Answer 1

up vote 3 down vote accepted

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}'
share|improve this answer
    
That also returns the php-fpm master process which I do not want –  menders65 Apr 13 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 at 14:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.