logrotate日志转储
简单点的方法:
写个定时任务:
cd /root/online
zip log.out.$date.zip log.out
> log.out
#find ./ -name *.zip -ctime +7 | xargs rm -f
1 工具目录
***系统开启selinux,logrotate会不生效***
linux默认会安装logrotate工具,自身的boot.log就是通过它分割转储的。
[root@webmaster log]# ll| grep boot
-rw-------. 1 root root 0 Jun 25 03:33 boot.log
-rw-------. 1 root root 81499 Jun 23 03:20 boot.log-20180623
-rw-------. 1 root root 28340 Jun 24 03:38 boot.log-20180624
-rw-------. 1 root root 0 Jun 25 03:33 boot.log-20180625
主配置文件为/etc/logrotate.conf,还有个目录/etc/logrotate.d/。系统会按应用,在这个目录中创建转储配置文件。
-rw-r--r-- 1 root root 160 Jan 31 2017 chrony
-rw-r--r-- 1 root root 194 Oct 20 2017 httpd
-rw-r--r-- 1 root root 868 Feb 4 10:33 mysql
-rw-r--r-- 1 root root 351 Oct 17 2017 nginx
-rw-r--r-- 1 root root 203 Mar 7 21:37 php-fpm
-rw-r--r--. 1 root root 136 Jun 10 2014 ppp
-rw-r--r-- 1 root root 224 May 10 2017 syslog
-rw-r--r-- 1 root root 100 Oct 18 2017 wpa_supplicant
-rw-r--r-- 1 root root 100 Jan 30 21:51 yum
2 转储配置
2.1 nginx
[root@webmaster logrotate.d]# cat nginx
/app/nginx/logs/*.log{
daily
missingok
rotate 30
compress
delaycompress
notifempty
dateext
sharedscripts
postrotate
if [ -f /app/nginx/logs/nginx.pid ]; then
kill -USR1 `cat /app/nginx/logs/nginx.pid`
fi
endscript
}
/app/nginx/logs/*.log
#文件匹配
daliy 按天分割
missingok 日志为空则不转储
rotate 30 转储周期30天,过期日志自动删除
compress 通过gz格式压缩, 文件名如:jab.com.log-20180625.gz
dateext 文件名按日期命令,如jab.com.log-20180625.gz
postrotate 转储后执行命令,这个是给nginx发送reopen log file信号。
3 logrotate命令
[root@webmaster logs]# logrotate -?
Usage: logrotate [OPTION...] <configfile>
-d, --debug Don't do anything, just test (implies -v)
-f, --force Force file rotation
-m, --mail=command Command to send mail (instead of `/bin/mail')
-s, --state=statefile Path of state file
-v, --verbose Display messages during rotation
-l, --log=STRING Log file
--version Display version information Help options:
-?, --help Show this help message
--usage Display brief usage message
在写完一个配置文件后,要用debug检查一下,然后用verbose或者force切割试一下。
[root@webmaster logrotate.d]# logrotate -d /etc/logrotate.d/nginx
reading config file /etc/logrotate.d/nginx
Allocating hash table for state file, size 15360 B Handling 1 logs rotating pattern: /app/nginx/logs/*.log after 1 days (30 rotations)
empty log files are not rotated, old logs are removed
considering log /app/nginx/logs/error.log
log does not need rotating (log has been already rotated)considering log /app/nginx/logs/jab.com.log
log does not need rotating (log has been already rotated)not running postrotate script, since no logs were rotated [root@webmaster logrotate.d]# logrotate -f /etc/logrotate.d/nginx
--end:所有参数参考--
compress 通过gzip 压缩转储以后的日志
nocompress 不做gzip压缩处理
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空的方式,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。
nocopytruncate 备份日志文件不过不截断
create mode owner group 轮转时指定创建新文件的属性,如create 0777 nobody nobody
nocreate 不建立新的日志文件
delaycompress 和compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
missingok 如果日志丢失,不报错继续滚动下一个日志
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使日志文件为空文件也做轮转,这个是logrotate的缺省选项。
notifempty 当日志文件为空时,不进行轮转
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
sharedscripts 运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。如果没有配置这个,那么每个日志轮转后都会执行一次脚本
prerotate 在logrotate转储之前需要执行的指令,例如修改文件的属性等动作;必须独立成行
postrotate 在logrotate转储之后需要执行的指令,例如重新启动 (kill -HUP) 某个服务!必须独立成行
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
dateext 使用当期日期作为命名格式
dateformat .%s 配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配合dateext使用,只支持 %Y %m %d %s 这四个参数
size(或minsize) log-size 当日志文件到达指定的大小时才转储,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem).
当日志文件 >= log-size 的时候就转储。 以下为合法格式:(其他格式的单位大小写没有试过)
size = 5 或 size 5 (>= 5 个字节就转储)
size = 100k 或 size 100k
size = 100M 或 size 100M
logrotate日志转储的更多相关文章
- cron 和anacron 、日志转储的周期任务
一.cron是开机自动启动的 [root@localhost ~]# chkconfig --list | grep "cron" crond 0:off 1:off 2:on 3 ...
- logrotate日志管理工具
一.概述 logrotate是一个Linux系统默认安装了的日志文件管理工具,用来把旧文件轮转.压缩.删除,并且创建新的日志文件.我们可以根据日志文件的大小.天数等来转储,便于对日志文件管理. log ...
- logrotate 日志轮询(转存)
1.建立/etc/logrotate.d/nginx文件[1] vim /etc/logrotate.d/nginx 2.写入如下内容: /var/log/nginx/*log { daily rot ...
- :Linux 系统日志管理 日志转储
Linux日志服务器设置 使用“@IP:端口”或“@@IP:端口”的格式可以把日志发送到远程主机上. 假设需要管理几十台服务器,每天的重要工作就是查看这些服务器的日志,可是每台服务器单独登录,并且查看 ...
- logrotate日志管理工具与split文件切割命令
概述 logrotate是一个Linux系统默认安装了的日志文件管理工具,用来把旧文件轮转.压缩.删除,并且创建新的日志文件.我们可以根据日志文件的大小.天数等来转储,便于对日志文件管理. logro ...
- 【运维工具】logrotate 日志管理神器
服务器经常会产生各种各样的日志文件,我们需要定期清理 日志的分类 系统日志 应用日志 系统日志 例如系统的history 历史信息 crontab的运行日志 一般系统日志系统都帮我们运维好了,不 ...
- logrotate 日志清理后 rsyslog中断问题
<pre name="code" class="html">随后配置logrotate的配置文件/etc/logrotate.conf,加入下面的内 ...
- logrotate日志处理
介绍 logrotate旨在简化生成大量日志文件的系统的管理.它允许日志文件的自动轮换.压缩.删除和邮件.每个日志文件可以每天.每周.每月处理,也可以在它变得太大时处理.通常,logrotate作为每 ...
- Logrotate日志轮巡missingok: 在日志轮循期间,任何错误将被忽略
Linux日志文件总管——logrotate 编译自:http://xmodulo.com/2014/09/logrotate-manage-log-files-linux.html ...
随机推荐
- 项目Beta冲刺(团队5/7)
项目Beta冲刺(团队5/7) 团队名称: 云打印 作业要求: 项目Beta冲刺(团队) 作业目标: 完成项目Beta版本 团队队员 队员学号 队员姓名 个人博客地址 备注 221600412 陈宇 ...
- 关于 truncate table 的一点学习札记
---下面整理笔记来之 itpub 的各位前辈的语录.这里做了一个汇总.仅供学习. truncate table后,oracle会回收表和其表中所在的索引到initial 大小,也就是初始分配的seg ...
- Leetcode(58)题解:Length of Last Word
https://leetcode.com/problems/length-of-last-word/ 题目: Given a string s consists of upper/lower-case ...
- 2017ACM/ICPC广西邀请赛 K- Query on A Tree trie树合并
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- linux以及git和maven常用命令
maven常用命令: clean install -Dmaven.test.skip -Ptest-lx (注意:test-lx是pom文件名) 其他 https://www.cnblogs.co ...
- 利用CoreTelephony获取用户当前网络状态(判断2G,3G,4G)
前言: 在项目开发当中,往往需要利用网络.而用户的网络环境也需要我们开发者去注意,根据不同的网络状态作相应的优化,以提升用户体验. 但通常我们只会判断用户是在WIFI还是移动数据,而实际上,移动数据也 ...
- 通过mysqldumpslow来分析日志
通过mysqldumpslow来分析日志. 将mysql加入到全局变量中!!! sudo vim /etc/profile # 添加Mysql export PATH=$PATH:/usr/local ...
- Kafka使用经验小结
本文尽量从一个使用者的角度去记录一些在实战当中使用Kfaka所需要关注的要点,这样可能会贴切更多的读者,本文并不会介绍太多的Kafka的一些架构层次设计的知识,因为网上已经有一大堆的重复搬运的资料任由 ...
- java中匹配中文的正则表达式
java中要匹配中文的正则表达式可以有两种写法:一是使用unicode中文码:二是直接使用汉字字符: 例: (1)String str = "晴"; String regexStr ...
- hdu-5718 Oracle(水题)
题目链接: Oracle Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...