简体   繁体   English

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

[英]Linux `mpstat` doesn't work as expected from a crontab script

I have a script running from root crontab:我有一个从 root crontab 运行的脚本:

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

The script has an mpstat output formatted and saved in CSV format in a file:该脚本有一个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

Output of the file:文件的输出:

# 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

When I run the command out of the script:当我从脚本中运行命令时:

# 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

I get this output:我得到这个输出:

# 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

Watch out for the column %user having values 7.33,7.33,7.33 which is missing out from previous output when the same line executes from script.当从脚本执行同一7.33,7.33,7.33 ,请注意具有值7.33,7.33,7.33的列%user ,该列会从先前的输出中丢失。 Why such weird behavior ?为什么会有这种奇怪的行为?

My Linux flavor is RHEL5.10 64 bits : kernel-2.6.18-371.el5我的 Linux 风格是RHEL5.10 64 bitskernel-2.6.18-371.el5

I know this is a bit old, but I just ran into this today.我知道这有点旧,但我今天刚遇到这个。 As you noted, sourcing the .bash_profile fixed the problem.正如您所指出的,采购 .bash_profile 解决了这个问题。 In the man page, I found this quote:在手册页中,我找到了这句话:

ENVIRONMENT The mpstat command takes into account the following environment variable:环境 mpstat 命令考虑了以下环境变量:

 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.

For me the major difference was a 12 vs 24 hour clock, hence needing to awk fields $4,$6 vs $3,$5.对我来说,主要区别是 12 小时制和 24 小时制,因此需要 awk 字段 $4、$6 和 $3、$5。

You need to use:您需要使用:

mpstat 1 1

Here the explanation why: mpstat with parameters That will give you only 1 line, and the averages at the bottom.这里解释为什么: 带参数的 mpstat这只会给你 1 行,以及底部的平均值。 Then, you need to parse that one line (line #4).然后,您需要解析那一行(第 4 行)。 Also, there is no need to parse the date (or time) given by mpstat , you can instead get the current date of the system using $(date +'FORMAT') .此外,无需解析mpstat给出的日期(或时间),您可以使用$(date +'FORMAT')获取系统的当前日期。

Here the script I made, and it works for me.这是我制作的脚本,它对我有用。 It creates a file if it doesn't exist, and then it adds the results from mpstat 1 1 .如果文件不存在,它会创建一个文件,然后添加来自mpstat 1 1的结果。 I use \\t as separator;我使用\\t作为分隔符; if you like you can use , and change the extension of the file to *.csv .如果您愿意,可以使用,并将文件的扩展名更改为*.csv You can then add this script to the crontab to run every x minutes.然后,您可以将此脚本添加到 crontab 以每 x 分钟运行一次。

#! /bin/bash
DIR=/somepath
FN=$(date +'CPU-%Y-%m.log')
FN=$DIR/$FN
if [ ! -f $FN ]
then
    touch $FN
    chmod 644 $FN
    printf "Server\tDate\t\t\t%%usr\t%%nice\t%%sys\t%%iowait\t%%irq\t%%soft\t%%steal\t%%guest\t%%gnice\t%%idle\n" > $FN
fi
DATE=$(date +'%Y-%m-%d %H:%M:%S')
SERVER="Jupyter"
STAT=$(mpstat 1 1 | awk 'NR==4{printf "%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t", $4, $5, $6, $7, $8, $9, $10, $11, $12, $13}')
echo -e "$SERVER\t$DATE\t$STAT" >> $FN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM