![无法使用 logrotate 发送日志](https://linux22.com/image/690526/%E6%97%A0%E6%B3%95%E4%BD%BF%E7%94%A8%20logrotate%20%E5%8F%91%E9%80%81%E6%97%A5%E5%BF%97.png)
使用 logrotate 每日轮换日志。想要通过电子邮件发送日志。轮换工作如预期般进行,但日志未通过电子邮件发送。我能够使用 mail() 从 PHP 生成电子邮件,但我不确定是否需要为 logrotate 设置其他服务。我也不确定 logrotate 邮件错误会报告在哪里。
谢谢。
详细信息:在 Ubuntu 14.04.4(Digital Ocean LAMP)上,使用 /etc/logrotate.d/apache2(配置)的 logrotate:
/var/www/html/logs/my.log {
daily
missingok
rotate 360
compress
delaycompress
notifempty
create
sharedscripts
mail [email protected]
mailfirst
}
答案1
确保您可以发送邮件
您的系统可以发送电子邮件吗?您可能希望安装并配置 Postfix。可能要转发到relayhost
中标识的另一台主机/etc/postfix/main.cf
。
您可以用 进行测试。echo test | mail -s test [email protected]
/usr/bin/mail
是 logrotate 默认使用的命令,因此如果测试一切正常,logrotate应该也工作。
logrotate
手册页摘录:
-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.
关于 /usr/bin/mail
/usr/bin/mail
是一个符号链接(以 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
在此系统上,你可以mail
使用以下命令安装
# apt-get install bsd-mailx
我检查了一下,这个包确实存在于 Ubuntu 14.04 中所以我觉得你做得不错。我认为你也可以使用这个mailutils
包作为替代方案。
检查您的邮件日志 ( /var/log/mail.log
)。如果邮件发往 Postfix,您应该能够准确地看到邮件中发生了什么。
logrotate
如果上述不是问题,你可以进行调试
您可以手动运行 logrotate,甚至可以应用-v
详细模式和-f
强制模式。您应该可以看到发生了什么。
# 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