1

I'm new to Linux Bash script and learning. I'm just wondering is it possible to redirect stderr to a file only if the stderr contains ERROR.

I am executing Hadoop Hive commands, which I put it in a Bash script file to schedule the process. The hive command generates a lot of logs, and I don't want to redirect the logs to a file every time, But if the log contains Errors, then I want to redirect the log to a file and want to mail the error file to someone.

Please let me know how to achieve this. Thanks in advance..

Regards, Jeeva

Kavitha
  • 185
  • 1
  • 4
  • 14
  • Possible duplicate of [How to pipe stderr, and not stdout?](http://stackoverflow.com/questions/2342826/how-to-pipe-stderr-and-not-stdout) – Alex Libov Oct 31 '16 at 23:38
  • 1
    @AlexL. How does that answer the question? He wants the redirection to be conditional on the content. – Barmar Oct 31 '16 at 23:44
  • that question describes how to pipe stderr specifically through grep.. so Jee should grep for `ERROR` – Alex Libov Oct 31 '16 at 23:47

2 Answers2

2

If I understand correctly, if an error occurs, you want to preserve the entire error log in a file (including lines that might not match your error-detection pattern.). I don't think there's any way to achieve what you want purely through I/O redirection.

Instead, you can unconditionally redirect stderr to its own file. Then, as a post-processing step, you can grep through that file to see if ERROR shows up, and depending on the outcome, either mail the file to someone, or delete it.

Jim Lewis
  • 43,505
  • 7
  • 82
  • 96
0

You have to use the stderr file descriptor which is 2

For example:

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

0.sh
  • 2,659
  • 16
  • 37