[英]using wget to download log file every 5 mins & detect changes
i am writing a bash script to accomplish the following.我正在编写一个 bash 脚本来完成以下任务。
wget "https://url-to-logs.org" -O new_log
if diff -q new_log old_log; then
echo "no new log entries to send."
else
echo "new log entries found, sending alert."
diff -u new_log old_log > new_entries
#some logic i have to take the output of "new_entries", properly format the text and send the alert.
rm -rf old_log new_entries
cp new_log old_log
rm -rf new_log
fi
there is one additional thing - every night at midnight the server hosting the logs deletes all entries and displays a blank file until new log entries are made for the new day.还有一件事 - 每晚午夜,托管日志的服务器都会删除所有条目并显示一个空白文件,直到为新的一天创建新的日志条目。
i guess i could always run a cron job at midnight to run "rm -rf" and "touch" the old_log file, but curious if an easier way to do this exists.我想我总是可以在午夜运行一个 cron 作业来运行“rm -rf”并“触摸”old_log 文件,但很好奇是否存在更简单的方法。
thanks in advance for any/all input and help.提前感谢任何/所有输入和帮助。
If your logs are not rotating - ie the old log is guaranteed to be the prefix of the new log, you can just use tail
to get the new suffix - something like this:如果您的日志没有轮换 - 即旧日志保证是新日志的前缀,您可以使用
tail
获取新后缀 - 如下所示:
tail -n+$(( $(wc -l old_log) + 1 )) new_log > new_entries
If there are no new lines in new_log
, the new_entries
file will be empty, which you can check using stat
or some other way.如果
new_log
中没有新行,则new_entries
文件将为空,您可以使用stat
或其他方式检查。
If your logs are rotating, you should first use grep
to check if the last line from the old log exists in the new log, and if not - assume the entire new log is new:如果您的日志正在轮换,您应该首先使用
grep
检查旧日志的最后一行是否存在于新日志中,如果不存在 - 假设整个新日志是新的:
if ! egrep -q "^$(tail -n1 old_log)\$" new_log; then cat new_log > new_entries; fi
If all the lines in your log file are unique then you could use grep
:如果日志文件中的所有行都是唯一的,那么您可以使用
grep
:
wget "https://url-to-logs.org" -O new_log || exit 1
if new_entries=$(grep -vxFf old_log new_log)
then
# format and send alert
printf '%s\n' "$new_entries"
fi
mv -f new_log old_log
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.