linux進程監控腳本(/proc/$pid/status)

進程信息監控:除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:[email protected]    
     


#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]# 

發佈了24 篇原創文章 · 獲贊 3 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章