在CentOs6.4下,用root許可權測試。
# cd ~ //進入home目錄
# vim .bashrc //編輯檔案,把下面程式碼放入地址
mem ()
{
top -n1 -b | head -n7 | sed '1,6d' && top -n1 -b | sed '1,7d' | grep --color=auto $1;
ps aux | grep --color=auto $1 | grep --color=auto -v grep | awk -F " " '{ sum += $6 } END { printf "Total Memory Usage: %.1f MB\n", sum/1024 }'
}
# source .bashrc //使命令生效
測試
~檢視PHP或者nginx的佔用記憶體~
[root@bogon ~]# mem php
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
24984 root 20 0 336m 8212 1640 S 0.0 0.8 0:00.03 php-fpm
24985 www 20 0 336m 7776 1192 S 0.0 0.8 0:00.01 php-fpm
24986 www 20 0 336m 7776 1192 S 0.0 0.8 0:00.01 php-fpm
24987 www 20 0 336m 7776 1192 S 0.0 0.8 0:00.05 php-fpm
24988 www 20 0 336m 7776 1192 S 0.0 0.8 0:00.03 php-fpm
24989 www 20 0 336m 7784 1196 S 0.0 0.8 0:00.00 php-fpm
24990 www 20 0 336m 7784 1196 S 0.0 0.8 0:00.01 php-fpm
24991 www 20 0 336m 78m 71m S 0.0 7.9 0:07.50 php-fpm
24992 www 20 0 336m 7784 1196 S 0.0 0.8 0:00.04 php-fpm
24993 www 20 0 336m 77m 70m S 0.0 7.8 0:09.19 php-fpm
24994 www 20 0 340m 84m 74m S 0.0 8.6 0:07.78 php-fpm
Total Memory Usage: 301.4 MB
[root@bogon ~]# mem nginx
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2824 root 20 0 40660 2684 488 S 0.0 0.3 0:00.01 nginx
2832 www 20 0 71380 27m 1124 S 0.0 2.8 0:00.99 nginx
2833 www 20 0 71380 22m 1036 S 0.0 2.3 0:00.46 nginx
2834 www 20 0 71380 29m 1180 S 0.0 3.0 0:02.92 nginx
2835 www 20 0 71380 29m 1120 S 0.0 3.0 0:11.48 nginx
Total Memory Usage: 111.2 MB