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.


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

 /var/www/html/logs/my.log {
  rotate 360
  mail me@mydomain.com

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
  • Thanks. This has narrowed my problem down: I am using sendmail (which is working), but logrotate is trying to use the default mail. Where can I tell logrotate to use sendmail? /etc/cron.daily/logrotate: #!/bin/sh # Clean non existent log file entries from status file cd /var/lib/logrotate test -e status || touch status head -1 status > status.clean sed 's/"//g' status | while read logfile date do [ -e "$logfile" ] && echo "\"$logfile\" $date" done >> status.clean mv status.clean status test -x /usr/sbin/logrotate || exit 0 /usr/sbin/logrotate /etc/logrotate.conf – user2876623 Sep 24 '16 at 19:39
  • @user2876623 The mail command sends mail to sendmail. Did you try it? What do you mean by default mail? – Ryan Babchishin Sep 24 '16 at 19:45
  • Ryan- I did not understand that. When I try to manually run logrotate using logrotate --force /etc/logrotate.d/apache2, I get error: mail command failed for... (but log is rotated). But 'echo testmessage | sendmail -s test steve@x.com' works. And 'echo testmessage | mail -s test steve@x.com' fails saying that the program 'mail' is not installed. So I thought that 'mail' in logrotate was doing the same thing, and I needed logrotate to use sendmail in place of mail. Any ideas on my error? And thanks. – user2876623 Sep 24 '16 at 20:18
  • @user2876623 /usr/bin/mail is a symlink. You can install different packages for it as it uses the alternatives system. I believe bsd-mailx is the right package for your OS. I'll update my answer with more info about this... – Ryan Babchishin Sep 24 '16 at 20:43
  • 1
    @user2876623 I don't know how echo testmessage | sendmail -s test steve@x.comis working for you as it is not correct syntax from what I can tell and remember from 15 years ago when people still used it. linux.die.net/man/8/sendmail.sendmail – Ryan Babchishin Sep 24 '16 at 20:54

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.