So far with:


tail -fn0 /var/log/dirsrv/redacted-domain/audit | \
while read line ; do
        echo "$line" | grep -C 5 -i "add: member"

        if [ $? = 0 ]
                 mail -s "New User Added" me@email.com > /dev/null
        echo "$line" | grep -C 5 -i "add: nsaccountlock"

        if [ $? = 0 ]
                 mail -s "Account Disabled" me@email.com > /dev/null

I want to take the contents of grep matches from the file /var/log/dirsrv/redacted-domain/audit and throw them into an e-mail to me@email.com. It feels redundant (and does not work regardless) to throw the grep statement into the email itself with the above loop.

Sorry, I should include a sample of what is being grepped:

time: 20200114133315 dn: cn=users,cn=groups,cn=accounts,dc=ipa,dc=domain,dc=com result: 0 changetype: modify add: member member: uid=example_user,cn=users,cn=accounts,dc=ipa,dc=domain,dc=com

I want to find a match of type add: member and return the entire ^ into an email.


You have two options.

One is to send email as you are doing (https://stackoverflow.com/questions/5155923/sending-a-mail-from-a-linux-shell-script)

cat /path/to/file | mail -s "your subject" me@email.com

I suppose you do this on regular basis, and you send this e-mail from some crontab script. In that case type at the top of your crontab MAILTO=me@email.com.

0 * * * * /path/to/your/script

In that case server will send an email where the content will be whatever your script outputs on standard output. If script prints nothing the e-mail will not be sent. In that way you do not have to deal with mail program in a script to send an e-mail. Just let your grep print matching lines and they will be sent via e-mail. The problem is, you cannot change the subject that way, but an additional line in content would also be OK, I guess.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.