bash - 如果文件在 5 分钟内增长超过一定大小,则发出警报

标签 bash scripting

我正在尝试编写一个脚本来检查文件在过去 5 分钟内增长了多少,如果在这段时间内增长超过 20MB,则将其写入日志。

到目前为止,我已经设法写了这个;

output="$HOME/log/logsize.output"                       # This file is where the values are written to before being compared
currentsize=$(stat '-c%s' "$HOME/log/input.log")        # This is the current size of the log file

    echo $currentsize >> $output

oldsize=$(sed 'x;$!d' < "$output")                      # Sets the previous reading as "oldsize" by pulling the second to last line of $output
difference=$(("$currentsize" - "$oldsize"))             # This is the difference in size between the current and previous readings


if $difference > "1999999"                              # Checks the difference, if greater than 1999999 write an error.
    then
    echo "Warning! Log File has exceeded the expected rate of growth. Please investigate." > "$HOME/log/logalert.log"
else
    echo "Log File is within expected parameters" > "$HOME/log/logalert.log"
fi

当我运行这个脚本时,这是我收到的输出;

line 23: "2910" - "2910": syntax error: operand expected (error token is ""2910" - "2910"")

解决了!

这是我最后为了让这个工作所做的事情

#!/bin/bash
#########################################################################
# Author - Jack Arnold
#
# Last Updated: 20.02.18
#########################################################################
#
# This script exists to periodically check the file size of a log file.
# If this file has grown 20MB or more since the last loop of this script
# it will write out an alert to ~/logs/logsize.log
#
#########################################################################


# Variables for the script.

output="$HOME/log/logsize.output"                       # This file is where the values are written to before being compared
currentsize=$(stat '-c%s' "$HOME/log/input.log")        # This is the current size of the log file

    echo "$currentsize" >> "$output"

oldsize=$(sed 'x;$!d' < "$output")                      # Sets the previous reading as "oldsize" by pulling the second to last line of $output
difference=$((currentsize - oldsize))                   # This is the difference in size between the current and previous readings


if [[ $difference > "1999999" ]]                        # Checks the difference, if greater than 1999999 write an error.
    then
    echo "Warning! Log File has exceeded the expected rate of growth. Please investigate." > "$HOME/log/logalert.log"
else
    echo "Log File is within expected parameters" > "$HOME/log/logalert.log"
fi

最佳答案

我注意到的第一件事是,在第一行中,变量赋值 ( output="~/log/logsize.output" ) 会导致问题,如 ~未在引号中展开。但是,此脚本中有很多错误,我建议您花更多时间学习 shell 脚本的基础知识;我建议 Greg’s Wiki作为一个很好的起点。

A while ago ,我更新了 使用指南标签,以便它包含检查 https://www.shellcheck.net/ 中的 shell 脚本的建议这是一个极好的资源。事实上,Shellcheck 会警告波浪号问题,并包含使用 $HOME 的有用建议。而不是 ~ .我不会重复 所有 Shellcheck 会警告您的问题,我只会提到一些它没有发现的问题:

命令替换

currentsize=(stat '-c%s' "~/log/input.log")

我想您打算使用命令替换,以便 currentsize变量包含 stat 的输出命令。这应该写成:

currentsize=$(stat '-c%s' "$HOME/log/input.log")

算术比较

Shellcheck 在到达此行之前停止处理,但我注意到您正在使用 >作为算术比较运算符:

if (${difference} > 1999999) ;

在 POSIX(类 Unix 操作系统的标准)shell 中,这些运算符用于输入和输出重定向——正如您在中所做的那样

echo "Log File is within expected parameters" > "~/log/logalert.log"

POSIX shell 中算术比较的正确运算符是 -gt测试这个的便携方法是:

if [ "$difference" -gt 1999999 ]

注意:壳如bashksh使用 < 扩展 POSIX和 >用于算术比较,但这仅适用于 double 括号。参见 Comparing integers: arithmetic expression or conditional expression .在 Bash 中,>[[ 一起使用时也可用于字符串比较构造。参见 Bash Conditional Expressions .

此外:如果字符串包含由 shell 专门解释的不寻常字符(例如,空格会导致分词),您才真正需要引用字符串。但是,如果您已经习惯使用其他编程语言并且发现它更具可读性,那么这样做也没有什么坏处。

一般提示

  • 始终引用变量扩展(除非您明确要求分词)
  • 使用set -x调试时。
  • set -e也可用于获得错误通知,例如尝试访问不存在的变量的内容。

关于bash - 如果文件在 5 分钟内增长超过一定大小,则发出警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48882998/

相关文章:

mysql - json 格式的输出数据库大小(来自 Centos7 的 Mariadb)

linux - 如何在 GNU/Linux cli 上使用 find regex 排除新行(回车)?

python - 更高效地编写 HTTP 脚本

php - PHP 脚本能否诱使浏览器认为 HTTP 请求已结束?

android - 用于安装多个 Android 应用程序的 Windows 脚本

bash - 使用 find 和 sed 递归重命名文件

python - 根据两列提取行数据

bash - 如何在 Dockerfile 中运行读取命令?

linux - 得到选择。 -help 参数不起作用。

perl - 在Windows中使用dos批处理程序的原因是什么?