The Dude abides.

Posted
14 August 2007

Tagged
Free Software
PHP
Technology

Collecting PHP (CLI) process stats via sh, awk and top

I found it necessary to collect PHP process statistics to be able to graph memory and CPU usage patterns over time. Collecting these process stats is easy enough with sh, top and awk. Witness the following (collecting memory usage statistics):

1
2
3
4
5
6
7
#!/bin/sh
 
while [ 0 ]
do
        top -b -n1 | grep php | awk '{sum=sum+$5} {shared=shared+$7}  END {print sum-shared, shared}' >> php.stats
        sleep 10
done

This script is easy enough to understand. Every 10 seconds, the script runs top in batch mode, extracts memory related information and appends it in a file (php.stats). To end script execution, hit CTRL+C. The contents of php.stats can then be read by a simple script to generate purty graphs.


cvsup.freebsdchina.org xkcd and Ender’s Game