在/etc/logrotate.d下创建一个文件,如:program
内容如下:
/home/ubuntu/program/program.log {
su root root
copytruncate
daily
rotate 5
compress
delaycompress
missingok
notifempty
create 644 root root
}
说明:
第一行:待打断日志的全路径
第二行:给program权限
第四行:按天打断
第五行:保留5天日志
可能会有多余的地方,我还没探索到,目前这样是可以成功打断的
sudo logrotate -d /etc/logrotate.d/program # 或者
sudo logrotate -vf /etc/logrotate.d/program
成功的情况下
即可看到/home/ubuntu/program/路径下的日志有所变化
日志打断失败,排查问题
报错:error: skipping "/home/ubuntu/program/program.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
问题解决:
报错大意就是需要使用su修改权限
需要在program里面加入su root root
sudo logrotate /etc/logrotate.conf报错:error: Ignoring program because of bad file mode - must be 0644 or 0444.
问题解决:
sudo chmod 644 /etc/logrotate.d/program # 修改权限
stat -c "%a" /etc/logrotate.d/program # 查看权限