Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm using logrotate to rotate my log files.

I've set it up to create new logfiles every week.

However I would like it to first mail me the logfiles as attachments.

The /var/log/httpd file currently contains:

/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    firstaction
        {I think I need to mail the logfiles here???}
    postrotate
        /sbin/service httpd reload > /dev/null 2>/dev/null || true
    endscript
}

Is it possible to do what I want?

If so: what do I need to add to firstaction for it to:

  • mail me the logfiles (/var/log/httpd/*log) as attachment using sendmail
  • add some subject to the mail, e.g.: HTTPD logfiles {week #}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

If I understand your question correctly, there's a logrotate option that will do what you want. Simply add mail user@yourdomain.com as one of the configuration options, like this:

/var/log/httpd/*log {
    mail user@yourdomain.com
    ...

This will mail you the log files that are about to be rotated.

Perhaps you've tried this and it didn't quite do what you need. In that case, I think you're on the right track for the most part. However, invoking sendmail directly isn't the right approach when you're trying to send a message with attachments. To quote the sendmail FAQ:

How do I create attachments with sendmail?

You don't. Sendmail is a mail transfer agent (MTA). Creating e-mail messages, including adding attachments or signatures, is the function of a mail user agent (MUA). Some popular MUAs include mutt, elm, exmh, Netscape, Eudora and Pine. Some specialized packages (metamail, some Perl modules, etc.) can also be used to create messages with attachments.

If you have the mail user agent mutt (or can install it), it should be able to do what you want. Try something like this:

/var/log/httpd/*log {
    firstaction
       echo | mutt -s "Log files for `date`" user@yourdomain.com -a /var/log/httpd/*log
    endscript
    ...

Note also in your example configuration file you're missing the endscript for your firstaction command.

share|improve this answer
    
echo | mutt -s "Log files for date" user@yourdomain.com -a /var/log/httpd/*log :: Doesn't it send raw log files without compression? How to mail .gz compressed files instead of .log files? Thank you! –  ThinkCode Sep 26 '12 at 16:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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