公司想制作网站吗,wordpress手机视频播放器,wordpress与typecho,网站建设方案标书文章目录 介绍安装配置默认配置logrotate参数 运行debug模式verbose模式force模式 定时执行cron.daily自定义crontab 配置示例按文件大小转储按日期转储 介绍
logrotate是基于linux系统的日志管理工具#xff0c;可用于切割、删除、转储日志文件#xff0c;logrotate是基于c… 文章目录 介绍安装配置默认配置logrotate参数 运行debug模式verbose模式force模式 定时执行cron.daily自定义crontab 配置示例按文件大小转储按日期转储 介绍
logrotate是基于linux系统的日志管理工具可用于切割、删除、转储日志文件logrotate是基于crontab运行的其配置文件为/etc/logrotate.conf也可以将自定义的配置文件放在/etc/logrotate.d目录下可覆盖logrotate.conf的配置。
安装
通常Linux默认安装了logrotate可以通过以下命令检查是否已经安装
rootserver:~# logrotate --version
logrotate 3.19.0Default mail command: /usr/bin/mailDefault compress command: /bin/gzipDefault uncompress command: /bin/gunzipDefault compress extension: .gzDefault state file path: /var/lib/logrotate/statusACL support: yesSELinux support: yes安装后其执行文件为/usr/sbin/logrotate其并不是系统服务和守护进程。
配置
默认配置
/etc/logrotate.conf为默认的配置文件其中的配置向对于所有的日志文件都生效如果要对特定的日志文件进行配置需要在/etc/logrotate.d/目录下单独添加配置文件。其默认配置如下
# see man logrotate for details# global options do not affect preceding include directives# rotate log files weekly
weekly# use the adm group by default, since this is the owning group
# of /var/log/syslog.
su root adm# keep 4 weeks worth of backlogs
rotate 4# create new (empty) log files after rotating old ones
create# use date as a suffix of the rotated file
#dateext# uncomment this if you want your log files compressed
#compress# packages drop log rotation information into this directory
include /etc/logrotate.d# system-specific logs may also be configured here.logrotate参数
参数说明daily/weekly/monthly/yearly日志轮换周期每天、每周、每月、每年size日志文件达到指定大小时轮换默认单位bytes如size 50M, size 1G。rotate日志文件的保留个数默认为0如rotate 5。maxage指定日志文件的最大存在时间。minsize指定日志文件的最小大小。maxsize指定日志文件的最大大小。olddirnoolddir将转出的日志文件存放到指令目录如olddir ./xxxx/默认为当前目录下。compressnocompress压缩旧日志文件。delaycompressnodelaycomporess在下一次轮换时才压缩日志文件。missingok如果日志文件不存在则忽略错误。notifempty如果日志文件为空则不轮换。copytruncate使用复制和截断来实现日志文件的轮换。dateext在轮换后的日志文件名中添加日期扩展默认为-%Y%m%d可用dataformat修改。dateformat自定义日期格式用于日志文件名的日期扩展配合dateext使用。extension指定压缩的日志文件扩展名。ifempty如果日志文件为空则仍然进行轮换。create创建新的日志文件并设置权限、所有者和组如create 744 root root。su指定以特定用户身份执行轮换操作。sharedscripts在每个日志文件轮换之后执行一次postrotate脚本。prerotateendscript在轮换之前执行特定的命令。postrotateendscript在轮换之后执行特定的命令。firstactionendscript在第一次轮换之前执行特定的命令。lastactionendscript在最后一次轮换之后执行特定的命令。
运行
debug模式
用于测试配置文件的正确性仅输出debug信息不执行操作。
logrotate -d 配置文件verbose模式
会根据配置执行操作并打印详细信息。
logrotate -v 配置文件force模式
测试时如果日期不达到配置中的轮换时间执行logrotate命令时也不会进行转储可以通过-f进行强制执行。
logrotate -f 配置文件定时执行
cron.daily
logrotate是基于cron运行的执行时间通过cron的配置进行设定其配置文件为/etc/crontab和/etc/anacrontab。
在/etc/cron.daily/目录下包含logrotate脚本每日执行logrotate命令如果自行配置了crontab可能导致转储操作执行两次。
cron.daily的执行时间配置在/etc/crontab中。
25 6 * * * root test -x /usr/sbin/anacron || ( cd / run-parts --report /etc/cron.daily )以上配置表示cron.daily在每天的6点25分执行。这样日志转储的时间也是6点25分。如果想不按照这个时间进行日志转储则logrotate配置文件不能放在/etc/logrotate.d/目录下否则将会执行两次。
自定义crontab
编辑 crontab 文件
crontab -e添加定时配置
* * * * * logrotate /root/logrotate/test该配置将每分钟执行一次转储操作。
配置示例
按文件大小转储
/var/log/test/*.log {size 30rotate 5copytruncatemissingokcompressdelaycompresscreate 0640 root root
}对于原始的test.log文件将被转储为文件名为test.log-20231223的文件且已转储的文件将被压缩成test.log-20231222.gz文件。
按日期转储
/var/log/test/*.log {dailyrotate 5copytruncatedateextmissingokcompressdelaycompresscreate 0640 root root
}按每日对日志文件进行转储时logrotate配置文件可以放在/etc/logrotate.d/目录下通过默认配置的cron.daily进行定时触发。