Shell script - 查看进程占用内存情况

导语

给定服务名称,查看服务对应的进程占用的内存情况

*** 用法 ***

lc_pmem service-name

*** 举例 ***

root@pts/0 $ lc_pmem mysql
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
25096 mysql     20   0 2875808 666836   7716 S  12.5 17.2 104:58.33 mysqld
24469 root      20   0  113248   1568   1272 S   0.0  0.0   0:00.17 mysqld_safe
685961216

*** 脚本***

#!/usr/bin/env bash
# -*- coding: utf-8 -*-
#Filename:  commFuncs.sh
#Author:        Liuchao
#Email:     137642091@qq.com
#Date:      2016-06-16
#Desc:      常用工具封装
#

source /devOps/shell/common/functions

lc_pmem(){
    pname="$1"
    ## list all [${pname}] related
    top -n1 -b | head -n7 | sed '1,6d' && top -n1 -b | sed '1,7d' | grep --color=auto $1;

    ## summary the total memory about [${pname}]
    ## 单位换成字节,方便zabbix监控
    ps aux | grep --color=auto $1 | grep --color=auto -v grep | awk -F " " '{ sum += $6 } END { print sum*1024 }'

    ## output with MB
    # 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 }'
}

## call function
if [ $# -eq  1 ]
then
    lc_pmem "$1"
else
    uLog "Please enter program-name like:"
    uLog "\tlc_pmem mysql"
fi

你可能感兴趣的