仅当日志包含ERROR

时间:2016-10-31 23:13:35

标签: linux bash hive

我是Linux Bash脚本和学习的新手。我只是想知道只有当stderr包含ERROR时才可以将stderr重定向到文件。

我正在执行Hadoop Hive命令,我把它放在Bash脚本文件中以安排进程。 hive命令生成大量日志,我不想每次都将日志重定向到文件,但如果日志包含错误,那么我想将日志重定向到文件并希望将错误文件邮寄到有人。

请告诉我如何实现这一目标。提前谢谢..

此致 Jeeva

2 个答案:

答案 0 :(得分:2)

如果我理解正确,如果发生错误,你想保留 整个错误记录在一个文件中(包括可能与您的错误检测模式不匹配的行)。我认为没有办法实现你的目标 纯粹希望通过I / O重定向。

相反,您可以无条件地将stderr重定向到自己的文件。然后, 作为后处理步骤,您可以浏览该文件以查看是否 ERROR显示,并根据结果,邮寄文件 某人,或删除它。

答案 1 :(得分:0)

您必须使用stderr文件描述符,即2

例如:

rm this-file-does-not-exist.txt 
>>>> rm: cannot remove ‘asdfasf.js’: No such file or directory
# to redirect that error to a file you do this
rm this-file-does-not-exist.txt 2>/tmp/logError.txt
cat /tmp/logError.txt
>>>> rm: cannot remove ‘asdfasf.js’: No such file or directory
# if you want to check if the output contains `ERROR` do this
badcommand | grep "ERROR" 1>/tmp/logError.txt # if greping for "ERROR" was successfull redirect stdout to /tmp/logError.txt
# the 1 is a file descriptor for stdout 

How to use linux mail command

相关问题