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