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 ...
随机推荐
- Node.js 爬虫批量下载美剧 from 人人影视 HR-HDTV
这两天发现了一个叫看知乎的站点.是知乎的苏莉安做的,当中爬虫使用的 Node.js.这里就针对上一篇博客中的美剧小爬虫,改用 nodejs 进行实现一下.体验一下强大的 Node.js. 假设之前没实 ...
- [网页游戏开发]Morn组件赋值
在讲解List之前,我们先介绍一下Morn组件赋值功能 默认属性赋值 界面逻辑开发过程中,经常会涉及到动态更改UI属性,比如: 界面有一个按钮,一个多选框和一个文本,分别命名为myButton,myC ...
- hadoop mapred和mapreduce包
mapred包是老的1.0的map reduce api mapreduce包是新的2.0的map reduce api
- https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-2.1.1-linux-x86_64.tar.bz2
- MySQL 权限生效
用GRANT.REVOKE或SET PASSWORD对授权表施行的修改会立即被服务器注意到. 如果你手工地修改授权表(使用INSERT.UPDATE等等),你应该执行一个FLUSH PRIVILEGE ...
- SD/MMC异同
该文章转自:http://www.imhan.com/archives/12/ 经常看到SD/MMC这样的写法,在这里稍微总结一下SD卡和MMC卡的异同点吧. 首先,两者在外型的规格上是几乎一致的.而 ...
- CentOS 7.2 安装Gerrit 2.14.6
1.环境 本文使用VMWare虚拟机进行实验. 2核CPU,4GB内存,20GB硬盘,IP:192.168.159.131 CentOS 7.2最小安装(CentOS-7-x86_64-Minimal ...
- Linux内核日志开关
Linux内核日志开关 1.让pr_debug能输出 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -59,7 +59,7 ...
- ssm使用velocity模板语言
1.在pom.xml里添加velocity模板语言支持的依赖 <!-- velocity模板语言支持包 --> <dependency> <groupId>org. ...
- 用/proc/stat计算cpu的占用率
转自:http://blog.csdn.net/pppjob/article/details/4060336 在Linux下,CPU利用率分为用户态,系统态和空闲态,分别表示CPU处于用户态执行的时间 ...