Shell starts another script for statistics. awk adds the current time in each row

Source: Internet
Author: User

Control script,

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

$ Indicates the PID of the currently running process, $! Indicates the PID of the last process.

Script for running the statistics program

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;

In the preceding script, the average value of the latest 10000 records is calculated every five seconds.

Print the current time on each line in the 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.