tail 和 grep 日志和邮件 (linux)

发布于 2024-10-11 03:08:37 字数 266 浏览 4 评论 0原文

我想用 grep 跟踪日志文件并通过邮件发送 比如:

tail -f /var/log/foo.log | grep error | mail -s subject [email protected]

我该怎么做?

i want to tail log file with grep and sent it via mail
like:

tail -f /var/log/foo.log | grep error | mail -s subject [email protected]

how can i do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别靠近我心 2024-10-18 03:08:37

您想在发送电子邮件发生错误时发送电子邮件吗?这可能会失败;)

但是您可以尝试这样的操作:

tail -f $log |
grep --line-buffered error |
while read line
do
    echo "$line" | mail -s subject "$email"
done

对于 grep 输出中的每一行都会发送一封电子邮件。

运行上面的 shell 脚本,

nohup ./monitor.sh &

这样它就会在后台继续运行。

You want to send an email when emailing errors occur? That might fail ;)

You can however try something like this:

tail -f $log |
grep --line-buffered error |
while read line
do
    echo "$line" | mail -s subject "$email"
done

Which for every line in the grep output sends an email.

Run above shell script with

nohup ./monitor.sh &

so it will keep running in the background.

狂之美人 2024-10-18 03:08:37

我会尝试一下这个。如果我讨厌的 bash 代码得到仔细检查,也许我会学到一些东西。有可能已经有无数的解决方案可以做到这一点,但我不会找出答案,因为我确信您已经拖网了网络海洋的深度和宽度。听起来你想要的可以分为两部分:1)定期获取文件的“最新尾部”,2)如果最新尾部确实存在,则通过电子邮件发送。对于 1) 中的定期间隔,请使用 cron。为了获得 2) 中的最新尾部,您必须跟踪文件大小。下面的 bash 脚本就是这样做的 - 它是 2) 的解决方案,可以由 cron 调用。它使用缓存的文件大小来计算需要邮寄的文件块。请注意,对于文件 myfile,会创建另一个文件 .offset.myfile。此外,该脚本不允许在文件名中包含路径部分。重写或在调用中修复它[例如(cd /foo/bar && segtail.sh zut),假设它被称为 segtail.sh ]。

#!/usr/local/bin/bash
file=$1
size=0
offset=0
if [[ $file =~ / ]]; then
   echo "$0 does not accept path components in the file name" 2>&1
   exit 1
fi
if [[ -e .offset.$file ]]; then
   offset=$(<".offset.$file")
   fi
if [[ -e $file ]]; then
   size=$(stat -c "%s" "$file")    # this assumes GNU stat, possibly present as gstat. CHECK!
                                   # (gstat can also be Ganglias Status tool - careful).
fi
if (( $size < $offset )); then     # file might have been reduced in size
   echo "reset offset to zero" 2>&1
   offset=0
fi
echo $size > ".offset.$file"
if [[ -e $file &&  $size -gt $offset ]]; then
   tail -c +$(($offset+1)) "$file" | head -c $(($size - $offset)) | mail -s "tail $file" foo@bar
fi

I'll have a go at this. Perhaps I'll learn something if my icky bash code gets scrutinised. There is a chance there are already a gazillion solutions to do this, but I am not going to find out, as I am sure you have trawled the depths and widths of the cyberocean. It sounds like what you want can be separated into two bits: 1) at regular intervals obtain the 'latest tail' of the file, 2) if the latest tail actually exists, send it by e-mail. For the regular intervals in 1), use cron. For obtaining the latest tail in 2), you'll have to keep track of the file size. The bash script below does that - it's a solution to 2) that can be invoked by cron. It uses the cached file size to compute the chunk of the file it needs to mail. Note that for a file myfile another file .offset.myfile is created. Also, the script does not allow path components in the file name. Rewrite, or fix it in the invocation [e.g. (cd /foo/bar && segtail.sh zut), assuming it is called segtail.sh ].

#!/usr/local/bin/bash
file=$1
size=0
offset=0
if [[ $file =~ / ]]; then
   echo "$0 does not accept path components in the file name" 2>&1
   exit 1
fi
if [[ -e .offset.$file ]]; then
   offset=$(<".offset.$file")
   fi
if [[ -e $file ]]; then
   size=$(stat -c "%s" "$file")    # this assumes GNU stat, possibly present as gstat. CHECK!
                                   # (gstat can also be Ganglias Status tool - careful).
fi
if (( $size < $offset )); then     # file might have been reduced in size
   echo "reset offset to zero" 2>&1
   offset=0
fi
echo $size > ".offset.$file"
if [[ -e $file &&  $size -gt $offset ]]; then
   tail -c +$(($offset+1)) "$file" | head -c $(($size - $offset)) | mail -s "tail $file" foo@bar
fi
深巷少女 2024-10-18 03:08:37

怎么样:

mail -s“catalina.out 错误”[电子邮件受保护] < grep 错误 catalina.out

How about:

mail -s "catalina.out errors" [email protected] < grep ERROR catalina.out

情释 2024-10-18 03:08:37

我在每 60 分钟运行一次的 cronjob 中使用 1-liner,检查文件在过去 60 分钟内是否已更改。如果是这样,日志文件的最后 3 行将发送给我。

log_file=$(find /var/log/mylog.log -mmin -60) && [[ -n "$log_file" ]] && tail -n 3 "$log_file" | mail -s "Alert mylog" [email protected]

如果您想发送包含单词“ERROR”的最后 3 行,如操作所要求的,您可以按如下方式修改:

log_file=$(find /var/log/mylog.log -mmin -60) && [[ -n "$log_file" ]] && grep "ERROR" "$log_file" | tail -n 3 | mail -s "Alert mylog" [email protected]

I use a 1-liner in my cronjob running every 60 minutes, checking if the file has changed within the last 60 minutes. If so, the last 3 lines of the log file are sent to me.

log_file=$(find /var/log/mylog.log -mmin -60) && [[ -n "$log_file" ]] && tail -n 3 "$log_file" | mail -s "Alert mylog" [email protected]

If you want to send the last 3 lines containing the word "ERROR", like requested by the op, you can modify this as follows:

log_file=$(find /var/log/mylog.log -mmin -60) && [[ -n "$log_file" ]] && grep "ERROR" "$log_file" | tail -n 3 | mail -s "Alert mylog" [email protected]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文