
Table of Contents
Scenario and Use Cases
You are running a PHP application on a Linux server and want to know how much RAM it is using.
Maybe you want to find how much memory your Flask application running via gunicorn is taking.
Or, if you just want to find the amount of RAM used by Nginx on Debian Linux.
Then, this blog post is for you.
Use ps to list the CPU and memory usage for php
ps command is a tool that allows you to view information about the processes running on your system. It provides details such as the process ID, CPU usage, memory usage and other things.
- process IDs pids
- CPU and memory usage
- the user who owns that process
- the terminal associated with the process
- the command that launched the process
- pocess state (running, sleeping, zombie)
You can use various options with the ps command to customize the output and filter the processes based on specific criteria. For example, you can use ps aux to display all processes with detailed information, or ps -ef to show a full-format listing of all processes. The ps command is commonly used for troubleshooting and monitoring system performance.
I personally use the ps command that suits my needs.
Find processes with ps -ef
ps -ef | grep php
Output:
$ ps -ef | grep php
root 25921 1 0 Mar18 ? 00:00:55 php-fpm: master process (/etc/php/8.5/fpm/php-fpm.conf)
www-data 181949 25921 0 Mar26 ? 00:05:36 php-fpm: pool www
www-data 194607 25921 0 Mar27 ? 00:02:58 php-fpm: pool www
www-data 201483 25921 0 Mar27 ? 00:01:14 php-fpm: pool www
arul 210332 210315 0 10:20 pts/0 00:00:00 grep php
Find processes with ps aux
$ ps aux | grep php
root 25921 0.0 0.8 226484 35652 ? Ss Mar18 0:55 php-fpm: master process (/etc/php/8.5/fpm/php-fpm.conf)
www-data 181949 0.2 1.1 301928 44816 ? S Mar26 5:36 php-fpm: pool www
www-data 194607 0.2 1.0 301844 40952 ? S Mar27 2:58 php-fpm: pool www
www-data 201483 0.1 0.9 301784 38800 ? S Mar27 1:14 php-fpm: pool www
arul 210330 0.0 0.0 6520 2268 pts/0 S+ 10:20 0:00 grep php
Find all php processes with size, username and command
$ ps -eo size,pid,command --sort -size | grep php
19404 181949 php-fpm: pool www
19344 201483 php-fpm: pool www
19320 194607 php-fpm: pool www
9764 25921 php-fpm: master process (/etc/php/8.5/fpm/php-fpm.conf)
Display the memory usage of php-fpm
We will use the previously used ps -eo command followed by awk. This will display the average php-fpm child process size.
ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%12.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | grep php-fpm|grep pool| awk '{sum+=$1; count++} END {print "php-fpm child process size:", sum/count, "MB"}'
Output:
$ ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%12.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | grep php-fpm|grep pool| awk '{sum+=$1; count++} END {print "php-fpm child process size:", sum/count, "MB"}'
php-fpm child process size: 18.93 MB
In my case, each PHP-FPM process is taking an average of 18.93MB per process.
Display the memory usage of gunicorn processes
ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%12.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | grep gunicorn | awk '{sum+=$1; count++} END {print "gunicorn child process size:", sum/count, "MB"}'
How much RAM is Nginx taking?
$ ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%12.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | grep nginx | awk '{sum+=$1; count++} END {print "nginx process size:", sum/count, "MB"}'
Output:
$ ps -eo size,pid,user,command --sort -size | awk '{ hr=$1/1024 ; printf("%12.2f Mb ",hr) } { for ( x=4 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }' | grep nginx | awk '{sum+=$1; count++} END {print "nginx process size:", sum/count, "MB"}'
nginx process size: 5.424 MB
Not too bad. Nginx has a very low footprint of only 5.4MB.
Conclusion
Hope this blog post helped you in some way. If you would like more scenarios involving memory usage of other processes, let me know and I'll add them to this blog post. Thanks for reading.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.