我定期运行“ps aux”,以以下格式输出值:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
如果我明白的话时间是累积的 CPU 时间,累积在某种意义上代表了该进程上花费的总 CPU 时间。在这里我假设这个值只能随着时间的消耗而增加。然而我发现自己处于一种实际价值的情况滴(虽然 PID 没有改变,所以这意味着进程没有重新启动)。因此我的问题是为什么会有这样的行为?
生成的数据(在最后一列之前有问题)的示例如下:
2017-08-04T07-05-01 user 16493 0.3 0.4 59536 28416 ? Sl Aug01 15:33 /usr/local/myprocess
2017-08-04T07-10-01 user 16493 0.3 0.4 59536 28416 ? Sl Aug01 15:34 /usr/local/myprocess
2017-08-04T07-15-01 user 16493 0.3 0.4 59536 28416 ? Sl Aug01 15:35 /usr/local/myprocess
2017-08-04T07-20-01 user 16493 0.3 0.4 59536 28416 ? Sl Aug01 15:37 /usr/local/myprocess
2017-08-04T07-25-01 user 16493 0.3 0.4 59536 28416 ? Sl Aug01 15:38 /usr/local/myprocess
2017-08-04T07-30-01 user 16493 0.3 0.4 59536 28416 ? Sl Aug01 15:39 /usr/local/myprocess
2017-08-04T07-35-01 user 16493 0.3 0.4 59536 28416 ? Sl Aug01 15:40 /usr/local/myprocess
2017-08-04T07-40-01 user 16493 0.0 0.4 60560 29608 ? Sl Aug01 1:21 /usr/local/myprocess
2017-08-04T07-45-01 user 16493 0.0 0.4 60560 29608 ? Sl Aug01 1:21 /usr/local/myprocess
2017-08-04T07-50-01 user 16493 0.0 0.4 60560 29608 ? Sl Aug01 1:21 /usr/local/myprocess
2017-08-04T07-55-01 user 16493 0.0 0.4 60560 29608 ? Sl Aug01 1:22 /usr/local/myprocess
2017-08-04T08-00-01 user 16493 0.0 0.4 60560 29608 ? Sl Aug01 1:22 /usr/local/myprocess
2017-08-04T08-05-01 user 16493 0.0 0.4 60560 29608 ? Sl Aug01 1:22 /usr/local/myprocess
2017-08-04T08-11-01 user 16493 0.0 0.4 60560 29608 ? Sl Aug01 1:22 /usr/local/myprocess
2017-08-04T08-15-01 user 16493 0.0 0.4 60560 29608 ? Sl Aug01 1:22 /usr/local/myprocess
2017-08-04T08-20-01 user 16493 0.0 0.4 60560 29608 ? Sl Aug01 1:22 /usr/local/myprocess
作为 cronjob 运行并生成我的数据的完整脚本是这个(只是实际的进程名称不同):
#!/usr/bin/env bash
proc_names="MyProcessToWatch1 MyProcessToWatch2 MyProcessToWatch3"
current_date=$(date "+%Y-%m-%dT%H-%M-%S")
hostname=$(hostname)
for proc_name in ${proc_names}
do
procs_to_watch=$(ps -ef | grep "${proc_name}" | grep -v grep | grep -v vi | grep -v tail | grep -v less | grep -v "ps -" | awk '{print $2}')
for proc_id in ${procs_to_watch}
do
proc_start_ts=$(ls -ld --time-style=+"%Y-%m-%dT%H-%M-%S" /proc/${proc_id} | awk '{ print $(NF-1) }')
log_file="/tmp/performance_data/performance_${proc_name}_${proc_start_ts}_${hostname}_${proc_id}.log"
echo -n "${current_date} " >> ${log_file} 2>&1
ps aux | grep "${proc_name}" | grep ${proc_id} >> ${log_file} 2>&1
done
done