[英]How to monitor multiple file through shell script
我想通过 shell 脚本监控Apache
和Tomcat
日志。
我可以通过脚本监控单个文件。 但是如何通过脚本监控多个文件?
我为单个文件编写了示例脚本。
#!/bin/bash
file=/root/logs_flow/apache_access_log
current=`date +%s`
last_modified=`stat -c "%Y" $file`
if [ $(($current-$last_modified)) -gt 180 ]; then
mail -s "$file is not updating proper" ramacn11@xx.xx.xxx
else
mail -s "$file is updating proper" ramacn11@xx.xxx.xxx
fi
我想用相同的脚本监控文件apache_error_log
和 tomcat 日志。
从您已经拥有的开始,一个简单的解决方案是使用要监控的文件作为参数调用您的脚本:
script.sh /root/logs_flow/apache_access_log
然后里面你放
file=$1
现在你可以把一堆这些放在 cron 中
* * * * * script.sh /root/logs_flow/apache_access_log
* * * * * script.sh /some/other/file.log
您可能想要稍微扩展脚本以检查参数是否已传递以及它是否是有效的文件名。
您可以使用find
命令列出一段时间内已更新或未更新的文件,这将比处理stat
的输出更便携,后者因操作系统而异。
以下将输出修改时间超过3分钟前的指定日志的名称:
find httpd.log tomcat.log -not -mtime -3m
或者,为了更轻松地管理文件列表,您可以使用 bash 数组:
#!/usr/bin/env bash
files=(
/root/logs_flow/apache_access_log
/var/log/tomcat.log
/var/log/www/apache-*.log # This is an expanding glob.
)
find "${files[@]}" -not -mtime -3m
如果阵列中的文件超过 3 分钟,则会列出它们。
一次从多个日志文件中读取..一个可以做
tail -f /home/user/log_A -f /home/user/log_B |egrep -v "^$|="
注意: egrep -v "^$|="
部分是为了从 tail 命令的输出中去除标题行和空行。 如果你想保留标题,你可以删除它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.