gpt4 book ai didi

Linux `mpstat` 无法按 crontab 脚本的预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 10:36:08 27 4
gpt4 key购买 nike

我有一个从根 crontab 运行的脚本:

05 * * * * /bin/bash /root/systat_kpi_tools/systat_kpi.sh

该脚本有一个 mpstat 输出格式并以 CSV 格式保存在一个文件中:

[[ -e ${BASE_DIR}/cpu_stat_${NOW}.csv ]] || echo "Year,Month,Day,Hr,Min,Sec,%user,%nice,%sys,%iowait,%irq,%soft,%steal,%idle,intr/s" >> ${BASE_DIR}/cpu_stat_${NOW}.csv
mpstat | awk 'NR>3{ print substr($0, index($0,$4)) }' | awk -v dt=$(date +'%Y,%m,%d,%H,%M,%S') -v OFS=, '{$1=dt OFS $1; print}' >> ${BASE_DIR}/cpu_stat_${NOW}.csv

文件输出:

# cat cpu_stat_160526.csv
Year,Month,Day,Hr,Min,Sec,%user,%nice,%sys,%iowait,%irq,%soft,%steal,%idle,intr/s
2016,05,26,14,05,01,0.00,2.17,0.08,0.01,0.14,0.00,90.29,1093.83
2016,05,26,15,05,02,0.00,2.19,0.08,0.01,0.14,0.00,90.25,1093.82

当我从脚本中运行命令时:

# echo "Year,Month,Day,Hr,Min,Sec,%user,%nice,%sys,%iowait,%irq,%soft,%steal,%idle,intr/s" > tmp
# mpstat | awk 'NR>3{ print substr($0, index($0,$4)) }' | awk -v dt=$(date +'%Y,%m,%d,%H,%M,%S') -v OFS=, '{$1=dt OFS $1; print}' >> tmp
# mpstat | awk 'NR>3{ print substr($0, index($0,$4)) }' | awk -v dt=$(date +'%Y,%m,%d,%H,%M,%S') -v OFS=, '{$1=dt OFS $1; print}' >> tmp
# mpstat | awk 'NR>3{ print substr($0, index($0,$4)) }' | awk -v dt=$(date +'%Y,%m,%d,%H,%M,%S') -v OFS=, '{$1=dt OFS $1; print}' >> tmp

我得到这个输出:

# cat tmp

Year,Month,Day,Hr,Min,Sec,%user,%nice,%sys,%iowait,%irq,%soft,%steal,%idle,intr/s
2016,05,26,15,04,54,7.33,0.00,2.19,0.08,0.01,0.14,0.00,90.25,1093.82
2016,05,26,15,04,57,7.33,0.00,2.19,0.08,0.01,0.14,0.00,90.25,1093.82
2016,05,26,15,04,58,7.33,0.00,2.19,0.08,0.01,0.14,0.00,90.25,1093.82

注意 %user 列的值 7.33,7.33,7.33 当从脚本执行同一行时,它在先前的输出中丢失了。为什么会有这种奇怪的行为?

我的 Linux 版本是 RHEL5.10 64 位:kernel-2.6.18-371.el5

最佳答案

我知道这有点老了,但我今天才遇到这个。正如您所指出的,采购 .bash_profile 解决了这个问题。在手册页中,我找到了这句话:

ENVIRONMENT The mpstat command takes into account the following environment variable:

  S_TIME_FORMAT
If this variable exists and its value is ISO then the current locale will be ignored when printing the date in the report header.
The mpstat command will use the ISO 8601 format (YYYY-MM-DD) instead.

对我而言,主要区别在于 12 小时制与 24 小时制,因此需要 awk 字段 $4、$6 与 $3、$5。

关于Linux `mpstat` 无法按 crontab 脚本的预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37457254/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com