gpt4 book ai didi

linux - cron.hourly 不工作

转载 作者:太空宇宙 更新时间:2023-11-04 03:43:04 25 4
gpt4 key购买 nike

我已经安排了 cron.hourly 作业来轮换 catalina.out 日志。我在 logrotate.d 中创建了一个名为 tomcat 的文件,其内容如下

/opt/apache-tomcat-7.0.55/logs/catalina.out {
copytruncate
daily
size 500k
rotate 3
compress
missingok
create 0600 root root
}

我还在/etc/cron.hourly 文件夹中创建了一个名为 logrotateTomcat 的文件,其中包含以下内容:

#!/bin/sh

/usr/sbin/logrotate /etc/logrotate.d/tomcat >/dev/null 2>&1
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

如果我运行命令/etc/cron.hourly/logrotateTomcat,它会旋转日志文件并压缩它。如果我手动运行多次,它还会保留旋转计数。但它不会自动执行相同的操作,我的日志条目如下所示在/var/log/cron 中,它表示任务已运行,但不会轮换文件。

ar 15 06:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[16107]: starting logrotateTomcat
Mar 15 06:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[16124]: finished logrotateTomcat
Mar 15 06:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[16107]: starting mcelog.cron
Mar 15 06:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[16139]: finished mcelog.cron
Mar 15 06:10:01 adaptapppsdev CROND[16273]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 06:20:01 adaptapppsdev CROND[16423]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 06:30:01 adaptapppsdev CROND[16567]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 06:40:01 adaptapppsdev CROND[16713]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 06:50:01 adaptapppsdev CROND[16859]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 07:00:01 adaptapppsdev CROND[17005]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 07:01:01 adaptapppsdev CROND[17025]: (root) CMD (run-parts /etc/cron.hourly)
Mar 15 07:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[17025]: starting 0anacron
Mar 15 07:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[17034]: finished 0anacron
Mar 15 07:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[17025]: starting logrotateTomcat
Mar 15 07:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[17042]: finished logrotateTomcat
Mar 15 07:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[17025]: starting mcelog.cron
Mar 15 07:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[17057]: finished mcelog.cron
Mar 15 07:10:01 adaptapppsdev CROND[17187]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 07:20:01 adaptapppsdev CROND[17333]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 07:30:02 adaptapppsdev CROND[17479]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 07:40:01 adaptapppsdev CROND[17625]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 07:50:01 adaptapppsdev CROND[17772]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 08:00:01 adaptapppsdev CROND[17954]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 08:01:01 adaptapppsdev CROND[17977]: (root) CMD (run-parts /etc/cron.hourly)
Mar 15 08:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[17977]: starting 0anacron
Mar 15 08:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[17986]: finished 0anacron
Mar 15 08:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[17977]: starting logrotateTomcat
Mar 15 08:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[17994]: finished logrotateTomcat
Mar 15 08:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[17977]: starting mcelog.cron
Mar 15 08:01:01 adaptapppsdev run-parts(/etc/cron.hourly)[18009]: finished mcelog.cron
Mar 15 08:10:01 adaptapppsdev CROND[18168]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 08:14:04 adaptapppsdev crontab[18230]: (root) LIST (root)
Mar 15 08:20:01 adaptapppsdev CROND[18327]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Mar 15 08:30:01 adaptapppsdev CROND[18473]: (root) CMD (/usr/lib64/sa/sa1 1 1)

如果我遗漏了什么,请帮助我。

最佳答案

SELinux 拒绝 logrotate 检查轮换日志文件的属性,当 logrotate 必须轮换/var/log(在 logrotate.conf 中定义)之外的文件时,会发生这种情况。logrotate 确实发送了一条记录器消息 logrotate: ALERT 由于 SELinux 而异常退出 [1],但它不会阻止 logrotate 成功完成。解决方案:

semanage fcontext -a -t var_log_t '/opt/<apachetomcat>/logs(/.*)?'

然后

cat /etc/selinux/targeted/contexts/files/file_contexts.local
# This file is auto-generated by libsemanage
# Do not edit directly.

opt/<apachetomcat>/logs(/.*)? system_u:object_r:var_log_t:s0

终于

# restorecon -Frvv opt/<apachetomcat>/logs

这将完成您在/var/log 之外轮转日志所需的一切

关于linux - cron.hourly 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29060681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com