3

I have tried the following command to check the an average memory usage by single PHP-FPM process

ps --no-headers -o "rss,cmd" -C php-fpm | awk '{ sum+=$1 } END { printf ("%d%s\n", sum/NR/1024,"M") }'

and I got an error awk: cmd. line:1: fatal: division by zero attempted

And this command

total=0; for i in `ps -C php-fpm -o rss=`; do total=$(($total+$i)); done; echo "Memory usage: $total kb";
Memory usage: 0 kb

ps -ef | grep php
root       9435      1  0 11:42 ?        00:00:00 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
somename+   9438   9435  0 11:42 ?        00:00:00 php-fpm: pool somename-1
somename+   9439   9435  0 11:42 ?        00:00:01 php-fpm: pool somename-1 
...
...

3 Answers 3

5

For the ps option -C to match, you need to make sure you're looking for the right string - not the extended command name shown by default with e.g. -ef, but what the c option shows in combination with e.g. ax.

Compare:

% ps -ef | grep '[f]'pm
www-data  3014  3714  2 19:09 ?        00:01:16 php-fpm: pool www
root      3714     1  0 Srp03 ?        00:00:32 php-fpm: master process (/etc/php/7.0/fpm/php-fpm.conf)
www-data 10644  3714  2 18:14 ?        00:02:35 php-fpm: pool www
www-data 18448  3714  2 19:42 ?        00:00:28 php-fpm: pool www

% ps axc | grep '[f]'pm
 3014 ?        S      1:17 php-fpm7.0
 3714 ?        Ss     0:32 php-fpm7.0
10644 ?        S      2:35 php-fpm7.0
18448 ?        S      0:28 php-fpm7.0

In this example, the string matchable with -C is php-fpm7.0:

% ps -C php-fpm7.0 -o rss=
32516
65952
61468
61012
5

I don't know where I found it, but it works!

ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | grep php-fpm

enter image description here

1
  • 1
    worked like a charm thank you May 31, 2022 at 8:36
0

For use on Alpine Linux, this command is based on the answer from Marc Garcia

ps -T -o rss,comm,pid,args | awk '{ hr=$1 ; printf("%13.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | grep php-fpm

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .