在进程监控脚本中,我们通常需要根据脚本的参数来确定有哪些性能参数将被收集,当这些性能参数大于最高阈值或小于最低阈值时,监控脚本将根据实际的情况,采取预置的措施,如邮件通知、直接杀死进程等,这里我们给出的例子是收集进程运行时长性能参数。
ps命令的etime值将给出每个进程的运行时长,其格式主要为以下三种:
1. minutes:seconds,如20:30
2. hours:minutes:seconds,如1:20:30
3. days-hours:minute:seconds,如2-18:20:30
该脚本将会同时处理这三种格式的时间信息,并最终转换为进程所流经的分钟数。
[root@xieqichao ~]
pid_string=`ps -eo pid,etime,comm | grep "init" | grep -v grep`
exec_time=`echo $pid_string | awk '{print $2}'`
time_field_count=`echo $exec_time | awk -F: '{print NF}'`
count_of_minutes=`echo $exec_time | awk -F: '{print $(NF-1)}'`
if [ $time_field_count -lt 3 ]; then
count_of_hours=0
count_of_days=0
else
count_of_hours=`echo $exec_time | awk -F: '{print $(NF-2)}'`
fields=`echo $count_of_hours | awk -F- '{print NF}'`
if [ $fields -ne 1 ]; then
count_of_days=`echo $count_of_hours | awk -F- '{print $1}'`
count_of_hours=`echo $count_of_hours | awk -F- '{print $2}'`
else
count_of_days=0
fi
fi
elapsed_minutes=`echo "$count_of_days*1440+$count_of_hours*60+$count_of_minutes" | bc`
echo "The elapsed minutes of init process is" $elapsed_minutes "minutes."
CTRL+D
[root@xieqichao ~]
The elapsed minutes of init process is 577 minutes.