控制指令碼,
stat.sh
PID_NUM=$$PID_FILE=stat.pid#echo "pid : "$PID_NUMPROG_NAME=$0ACTION=$1usage() { echo "Usage: ${PROG_NAME} {start|stop|restart}" exit 1;}start(){ sh ./stat_run.sh & PID_NUM=$! echo "worker pid is " $PID_NUM echo ${PID_NUM} > ${PID_FILE}}stop(){ PID_NUM=`cat ${PID_FILE}` if [ "" != "${PID_NUM}" ]; then echo kill pid is "${PID_NUM}" kill ${PID_NUM} fi}case "${ACTION}" in start) start ;; stop) stop ;; restart) stop sleep 1 start ;; *) usage ;;esac
其中$$ 表示當前正在啟動並執行進程的pid,$!表示最近的上一個進程pid
真正運行統計程式的指令碼
cat stat_run.sh STAT_FILE=stat_delay.log echo "" > $STAT_FILE while true; do tail -n10000 perf.log | awk '{print $8}' | grep totalAll | awk -F, 'BEGIN {cnt=0;total=0} {cnt=cnt+1;total=total+$3} END{print '"\"$(date +%Y-%m-%d-%H:%M:%S)\""', total/cnt}' >> $STAT_FILE; sleep 5; done;
上面指令碼,每5秒統計最新產生的10000條記錄的平均值
在awk中每行列印目前時間
[admin@v021130.sqa.cm4 logs]$ cat test.log 2013-07-30-23:00:12 44.57912013-07-30-23:00:17 44.57912013-07-30-23:00:22 44.57912013-07-30-23:00:27 44.57912013-07-30-23:00:32 44.57912013-07-30-23:00:37 44.57912013-07-30-23:00:42 44.57912013-07-30-23:00:47 44.57912013-07-30-23:00:52 44.57912013-07-30-23:00:58 44.5791[admin@v021130.sqa.cm4 logs]$ awk '{print '"\"$(date +%Y-%m-%d-%H:%M:%S)\""', $1}' test.log 2013-07-30-23:01:21 2013-07-30-23:00:122013-07-30-23:01:21 2013-07-30-23:00:172013-07-30-23:01:21 2013-07-30-23:00:222013-07-30-23:01:21 2013-07-30-23:00:272013-07-30-23:01:21 2013-07-30-23:00:322013-07-30-23:01:21 2013-07-30-23:00:372013-07-30-23:01:21 2013-07-30-23:00:422013-07-30-23:01:21 2013-07-30-23:00:472013-07-30-23:01:21 2013-07-30-23:00:522013-07-30-23:01:21 2013-07-30-23:00:58[admin@v021130.sqa.cm4 logs]$ awk '{print '"\"$(date +%Y-%m-%d-%H:%M:%S)\""', $2}' test.log 2013-07-30-23:01:27 44.57912013-07-30-23:01:27 44.57912013-07-30-23:01:27 44.57912013-07-30-23:01:27 44.57912013-07-30-23:01:27 44.57912013-07-30-23:01:27 44.57912013-07-30-23:01:27 44.57912013-07-30-23:01:27 44.57912013-07-30-23:01:27 44.57912013-07-30-23:01:27 44.5791