Home > Blockchain >  output tail command to mail
output tail command to mail

Time:04-06

I am trying to get updates to /var/log/apache2/access.log and pass them to grep for certain criteria. If there is a match, I want to send the hit my the mail command. As a test, I am just trying to load "spacer.gif" in my web root. The grep search searches for access log entries with "spacer.gif" in them:

tail -f /var/log/apache2/access.log | grep spacer.gif

This works and I see the log entries containing "spacer.gif" but no others.

I then tried:

tail -f /var/log/apache2/access.log | grep spacer.gif | mail -s 'Log Entry found' [email protected]

I never get an email with the above. I can confirm that

echo "this is test output" | mail -s 'this is a test' [email protected]

does go through correctly.

Sorry, not very good with linux, obviously. I'm on Ubuntu 20.X

I have seen similar questions on StackOverflow but these aren't working either. Thanks for any help?

CodePudding user response:

Your tail command never terminates (due to -f). Therefore the input to mail nevers closes. Therefore mail does not know when it is OK to send the mail.

If you would kill the tail command, mail should complete its work.

CodePudding user response:

Maybe something like this:

tail -f /var/log/apache2/access.log | grep spacer.gif > /tmp/access.out;mail -s "Access Log" [email protected] < /tmp/access.out

  • Related