linux进程监控脚本(/proc/$pid/status)

来源:互联网 发布:网络数字矩阵 编辑:程序博客网 时间:2024/06/01 09:21
进程信息监控:除free top外,最靠谱的就是 /proc/$pid/status,该文件会实时记录进程的
内存占用、文件占用、信号处理等各种资源占用状况,具体内容看参照相关文档


下面的源码主要是实现了一个进程监控脚本,定时记录给定pid进程的信息,以供后期分析
为考虑可移植性即嵌入式环境的使用,脚本没有采用外部的命令

[root@linux mem_monitor]# cat process_monitor.sh 

#!/bin/sh
#process_monitor.sh monitor a process  statics by pid 


#Copyright (C) <2013> Jiancheng Li
#Written by Jiancheng Li ,August 2013.
#e-mail:jiancheng.leei@gmail.com    
     


#This program is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.


#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.


#You should have received a copy of the GNU General Public License
#along with this program.  If not, see <http://www.gnu.org/licenses/>.


options=${1:-"-h"}
#if [[ "x$options" == "x-h" || "x$1" != "x-pid" || "x$2" != x[0-9]*  ]]
if [[ "x$options" == "x-h" ]]
then
    echo "usage:`basename $0` -p process_pid -t seconds -l logfilename"
fi


while getopts ":p:t:l:" Option
do
    case $Option in
        p) process_pid=$OPTARG;;
        t) interval_time=$OPTARG;;
        l)  log_filename=$OPTARG;;
    esac
    #shift
done


if [[ "x$process_pid" != x[0-9]* || ! -d "/proc/$process_pid" ]]
then
    echo "usage:`basename $0` -p process_pid -t seconds -l logfilename"
    echo "no valid process pid"
    exit 1
fi


interval_time=${interval_time:-30}
log_filename=${log_filename:-"$process_pid"info.log}


echo "process pid: $process_pid"
echo "interval time: $interval_time"
echo "log filename: $log_filename"


:>$log_filename


trap 'echo "process $process_pid  runtime info wrote into $log_filename"' EXIT 
echo "`basename $0`_scriptpid:""$$">$log_filename
status_count=0
while [ -d "/proc/$process_pid" ]
do
    ((status_count+=1))
    echo "$status_count" >>$log_filename
    cat /proc/$process_pid/status>>$log_filename 
    sleep $interval_time
done




exit 0
[root@linux mem_monitor]# ./process_monitor.sh -p 256 
process pid: 256
interval time: 30
log filename: 256info.log
process 256  runtime info wrote into 256info.log


[root@linux mem_monitor]# cat 256info.log 
process_monitor.sh_scriptpid:12973
1
Name:   cqueue/7
State:  S (sleeping)
SleepAVG:       58%
Tgid:   256
Pid:    256
PPid:   83
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
FDSize: 64
Groups:
Threads:        1
SigQ:   4/49152
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: ffffffffffffffff
SigIgn: 0000000000010000
SigCgt: 0000000000000000
CapInh: 0000000000000000
CapPrm: 00000000ffffffff
CapEff: 00000000fffffeff
Cpus_allowed:   00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000080
Mems_allowed:   00000000,00000002
2
Name:   cqueue/7
State:  S (sleeping)
SleepAVG:       58%
Tgid:   256
Pid:    256
PPid:   83
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
FDSize: 64
Groups:
Threads:        1
SigQ:   4/49152
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: ffffffffffffffff
SigIgn: 0000000000010000
SigCgt: 0000000000000000
CapInh: 0000000000000000
CapPrm: 00000000ffffffff
CapEff: 00000000fffffeff
Cpus_allowed:   00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000080
Mems_allowed:   00000000,00000002
3
Name:   cqueue/7
State:  S (sleeping)
SleepAVG:       58%
Tgid:   256
Pid:    256
PPid:   83
TracerPid:      0
Uid:    0       0       0       0
Gid:    0       0       0       0
FDSize: 64
Groups:
Threads:        1
SigQ:   4/49152
SigPnd: 0000000000000000
ShdPnd: 0000000000000000
SigBlk: ffffffffffffffff
SigIgn: 0000000000010000
SigCgt: 0000000000000000
CapInh: 0000000000000000
CapPrm: 00000000ffffffff
CapEff: 00000000fffffeff
Cpus_allowed:   00000000,00000000,00000000,00000000,00000000,00000000,00000000,00000080
Mems_allowed:   00000000,00000002
[root@linux mem_monitor]# 

原创粉丝点击