Can not mail log with logrotate

emaillogrotate

Using logrotate to rotate logs daily. Want to email the log. The rotate is working as expected, but logs are not being emailed. I am able to generate emails from PHP using mail(), but I am not sure if I need to set up something else as a service for logrotate. I am also not sure where the logrotate mail error would be reported.

Thanks.

Details:
On Ubuntu 14.04.4 (Digital Ocean LAMP), using logrotate with /etc/logrotate.d/apache2 (config):

 /var/www/html/logs/my.log {
  daily
  missingok
  rotate 360
  compress
  delaycompress
  notifempty
  create
  sharedscripts
  mail me@mydomain.com
  mailfirst
 }

Best Answer

Make sure you can send mail

Can your system send email? You'll likely want Postfix installed and configured. Probably to relay to another host identified by relayhost in /etc/postfix/main.cf.

You can test with echo test | mail -s test user@example.com.

/usr/bin/mail is the command logrotate uses by default, so if things work in your test, logrotate should work too.

logrotate man page excerpt:

   -m, --mail <command>
          Tells logrotate which command to use when mailing logs. This command should accept two arguments: 1) the subject of the message, and 2) the recipient. The command must then read a message on standard input and mail it
          to the recipient. The default mail command is /usr/bin/mail -s.

About /usr/bin/mail

/usr/bin/mail is a symlink (example from Ubuntu 16.04):

# ls -l /etc/alternatives/mail
lrwxrwxrwx 1 root root 18 May 26 09:37 /etc/alternatives/mail -> /usr/bin/bsd-mailx
# dpkg -S /usr/bin/bsd-mailx
bsd-mailx: /usr/bin/bsd-mailx
# ls -l /usr/bin/mail
lrwxrwxrwx 1 root root 22 May 26 09:37 /usr/bin/mail -> /etc/alternatives/mail
# ls -l /etc/alternatives/mail
lrwxrwxrwx 1 root root 18 May 26 09:37 /etc/alternatives/mail -> /usr/bin/bsd-mailx
# dpkg -S /usr/bin/bsd-mailx
bsd-mailx: /usr/bin/bsd-mailx

On this system, you could install the mail command with

# apt-get install bsd-mailx

I checked and this package does exist in Ubuntu 14.04 so I think you're good. I think you can also use the mailutils package as an alternative.

Check your mail log (/var/log/mail.log). You should see exactly what is happening with the messages if they are making it to Postfix.


You can debug logrotate if the above isn't the problem

You can run logrotate manually and even apply -v for verbose mode and -f for force. You should see what's going on.

# logrotate -v -f mysql-server 
reading config file mysql-server

Handling 1 logs

rotating pattern: /var/log/mysql.log /var/log/mysql/*log  forced from command line (7 rotations)
empty log files are rotated, old logs are removed
considering log /var/log/mysql.log
  log /var/log/mysql.log does not exist -- skipping
considering log /var/log/mysql/error.log
  log needs rotating
rotating log /var/log/mysql/error.log, log->rotateCount is 7
dateext suffix '-20160924'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /var/log/mysql/error.log.7.gz to /var/log/mysql/error.log.8.gz (rotatecount 7, logstart 1, i 7), 
renaming /var/log/mysql/error.log.6.gz to /var/log/mysql/error.log.7.gz (rotatecount 7, logstart 1, i 6), 
renaming /var/log/mysql/error.log.5.gz to /var/log/mysql/error.log.6.gz (rotatecount 7, logstart 1, i 5), 
renaming /var/log/mysql/error.log.4.gz to /var/log/mysql/error.log.5.gz (rotatecount 7, logstart 1, i 4), 
renaming /var/log/mysql/error.log.3.gz to /var/log/mysql/error.log.4.gz (rotatecount 7, logstart 1, i 3), 
renaming /var/log/mysql/error.log.2.gz to /var/log/mysql/error.log.3.gz (rotatecount 7, logstart 1, i 2), 
renaming /var/log/mysql/error.log.1.gz to /var/log/mysql/error.log.2.gz (rotatecount 7, logstart 1, i 1), 
renaming /var/log/mysql/error.log.0.gz to /var/log/mysql/error.log.1.gz (rotatecount 7, logstart 1, i 0), 
old log /var/log/mysql/error.log.0.gz does not exist
renaming /var/log/mysql/error.log to /var/log/mysql/error.log.1
creating new /var/log/mysql/error.log mode = 0640 uid = 107 gid = 4
running postrotate script
compressing log with: /bin/gzip
removing old log /var/log/mysql/error.log.8.gz