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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,306评论 19 139
  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 14,178评论 2 33
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,951评论 25 709
  • 两个男人,当听说离生命的结束最多还有半年或一年时,他们沉默了,开始策划遗愿清单,两个垂危的老人,选择更刺激的活动,...
    心我听你说阅读 2,187评论 0 0
  • 对一个残疾人来说,是很不方便的,而在生活上也会有很大的困难,所以我们要尽自己的努力来给以她们帮助。 今天2017年...
    七彩夕阳阅读 3,030评论 0 0