email - tail 和 grep 日志和邮件 (linux)

标签 email shell grep tail

我想用 grep 拖尾日志文件并通过邮件发送
喜欢:

tail -f /var/log/foo.log | grep error | mail -s subject name@example.com

我怎样才能做到这一点?

最佳答案

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

但是,您可以尝试以下操作:

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

为 grep 输出中的每一行发送一封电子邮件。

使用上面的 shell 脚本运行
nohup ./monitor.sh &

所以它会一直在后台运行。

关于email - tail 和 grep 日志和邮件 (linux),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4656886/

相关文章:

PHP表单向多个收件人发送电子邮件

javascript - 使用 jQuery 和 php 执行两个任务

bash - 将方法传递给heredocs

c - 从命令行将输出重定向到 C 中的文本文件

bash - uniq - 比较行时跳过最后 N 个字符/字段

regex - 从egrep打印输出中排除以空格开头的行

php - Codeigniter 无法连接到 ssl ://smtp. googlemail.com 错误

javascript - 如何将下拉菜单和文本值传递到我的电子邮件?

regex - BBEdit Grep/Regex 查找并替换破折号后的部分

linux - 带参数运行 shell 脚本