赞
踩
涉及两块,一块是获取资源消耗信息,一块是定时采集。
获取资源消耗用到了top,ps等命令,脚本如下
PROCESS_NAME=$1 SEPARATOR="\t\t" PROCESS_ID=`ps -ef|grep $PROCESS_NAME |grep -v grep|grep -v PPID|awk '{ print $2}'` # ps -o pcpu is the average cpu from start, not what we want # -n should be more than 1 as the first sample is always 0, bug of top? INFO=`top -p $PROCESS_ID -b -n 2 | grep $PROCESS_ID|awk '{ print $5"\t"$6"\t"$9}'` # VIRT:virtual memory usage # RES:resident memory usage # SHR:shared memory 共享内存 # skip the result of the first time VIRT=`echo $INFO|awk '{print $4}'` RES=`echo $INFO|awk '{print $5}'` CPU=`echo $INFO|awk '{print $6}'` # Threads THREADS=`ps -mq $PROCESS_ID | grep -c - -` #handles HANDLES=`ls /proc/$PROCESS_ID/fd | wc -l` # time CURRENT=`date "+%Y-%m-%d %H:%M:%S"` #output #echo -e "Time${SEPARATOR}{SEPARATOR}PID${SEPARATOR}${SEPARATOR}VIRT${SEPARATOR}RES${SEPARATOR}CPU${SEPARATOR}Threads${SEPARATOR}Handles" echo -e "$CURRENT$SEPARATOR$PROCESS_ID$SEPARATOR$VIRT$SEPARATOR$RES$SEPARATOR$CPU$SEPARATOR$THREADS$SEPARATOR$HANDLES"
一块是定时采集,需要用到crontab
crontab -e可以直接编辑需要执行的任务,编辑之后crond会自动reload,任务参考如下:
* * * * * /root/scripts/monitor.sh
表示每分钟执行一次monitor.sh脚本
可以通过crontab -l查看是否已经创建的任务。
cron日志:/var/log/cron,可以确认脚本执行情况。
定时任务定义参考:Linux定时任务Crontab命令详解
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。