shell啟動另一個指令碼進行統計,awk在每一行添加目前時間

來源:互聯網
上載者:User

控制指令碼,

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.