logrotate没有rotate的排查过程
前言
方式一、Crontab模式
systemctl status cron.service
#cat /etc/crontab 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
Dec 8 06:25:01 (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ))
#!/bin/sh # skip in favour of systemd timer if [ -d /run/systemd/system ]; then exit 0 fi # this cronjob persists removals (but not purges) if [ ! -x /usr/sbin/logrotate ]; then exit 0 fi /usr/sbin/logrotate /etc/logrotate.conf EXITVALUE=$? if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]" fi exit $EXITVALUE
因为我的squid的定时切割是每天,所以需要/etc/cron.daily;同样可以手动执行(bash -x logrotate)一次这个脚本,看是否能成功,在这里,发现我的脚本在执行第一个检查的时候这一步退出了;然后花了大量的时候查找为什么会做这一步检查,后来经过网上的搜索发现/run/systemd/system这个目录下放着与时间相关的Systemd任务,所以这一步的判断是为了检测是否用了方式二Systemd timer做了定时任务,如果有这个目录,则不再做Crontab的定时任务;
● logrotate.service - Rotate log files Loaded: loaded (/lib/systemd/system/logrotate.service; static; vendor preset: enabled) Active: failed (Result: exit-code) since Wed 2021-12-08 00:00:03 +08; 16h ago TriggeredBy: ● logrotate.timer Docs: man:logrotate(8) man:logrotate.conf(5) Main PID: 2811680 (code=exited, status=1/FAILURE) Dec 08 00:00:01 systemd[1]: Starting Rotate log files... Dec 08 00:00:03 logrotate[2811680]: error: failed to rename /usr/squid/logs/access.log to /usr/squid/logs/access.log-20211208: Read-only file system
方式二:Systemd模式
● logrotate.service - Rotate log files Loaded: loaded (/lib/systemd/system/logrotate.service; static; vendor preset: enabled) Active: failed (Result: exit-code) since Wed 2021-12-08 00:00:03 +08; 16h ago TriggeredBy: ● logrotate.timer Docs: man:logrotate(8) man:logrotate.conf(5) Main PID: 2811680 (code=exited, status=1/FAILURE) Dec 08 00:00:01 systemd[1]: Starting Rotate log files... Dec 08 00:00:03 logrotate[2811680]: error: failed to rename /usr/squid/logs/access.log to /usr/squid/logs/access.log-20211208: Read-only file system
● logrotate.timer - Daily rotation of log files Loaded: loaded (/lib/systemd/system/logrotate.timer; enabled; vendor preset: enabled) Active: active (waiting) since Thu 2021-08-05 11:08:00 +08; 4 months 3 days ago Trigger: Thu 2021-12-09 00:00:00 +08; 7h left Triggers: ● logrotate.service Docs: man:logrotate(8) man:logrotate.conf(5)
[Unit] Description=Daily rotation of log files Documentation=man:logrotate(8) man:logrotate.conf(5) [Timer] OnCalendar=daily AccuracySec=12h Persistent=true #Unit:真正要执行的任务,默认是同名的带有.service后缀的单元 [Install] WantedBy=timers.target
待加强的知识点
1)anacron与cron的区别?
2)systemd创建定时任务与Cron创建定时任务的区别,优缺点?
3)systemd的配置以及整个systemd需要加强理解,系统学习
4)crontab定时任务的系统学习
5)logrotate系统的学习,配置等
6)看logrotate源码,用Python或Go模拟
最后
欢迎大家关注我的公众号,一起交流、学习。
-------------------------------------------
个性签名:在平凡中坚持前行,总有一天会遇见不一样的自己!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!
微信公众号 微信打赏 支付宝打赏
posted on 2021-12-09 21:56 Captain_Li 阅读(1893) 评论(0) 编辑 收藏 举报