发布时间:2015-09-13 22:06:03作者:知识屋
/usr/bin/logrotate 程序所在位置;/etc/cron.daily/logrotate 默认让Cron每天执行logrotate一次;/etc/logrotate.conf 全局配置文件;/etc/logrotate.d 应用自个的配置文件存放目录,覆盖全局配置;全局配置文件/etc/logrotate.conf# cat /etc/logrotate.conf# see "man logrotate" for details# rotate log files weeklyweekly# keep 4 weeks worth of backlogsrotate 4# create new (empty) log files after rotating old onescreate# use date as a suffix of the rotated file# dateext# uncomment this if you want your log files compressed#compress# RPM packages drop log rotation information into this directoryinclude /etc/logrotate.d# no packages own wtmp and btmp -- we'll rotate them here/var/log/wtmp { monthly create 0664 root utmp minsize 1M rotate 1}/var/log/btmp { missingok monthly create 0600 root utmp rotate 1}# system-specific logs may be also be configured here.
# cat /etc/logrotate.d/sssd/var/log/sssd/*.log { weekly missingok notifempty sharedscripts rotate 2 compress postrotate /bin/kill -HUP `cat /var/run/sssd.pid 2>/dev/null` 2> /dev/null || true endscript}
# cat /etc/cron.daily/logrotate#!/bin/sh/usr/sbin/logrotate /etc/logrotate.conf >/dev/null 2>&1EXITVALUE=$?if [ $EXITVALUE != 0 ]; then /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"fiexit 0
# 1. 调试 (d = debug)参数为配置文件,不指定则执行全局配置文件logrotate -d /etc/logrotate.d/test.conf# 2. 强制执行(-f = force),可以配合-v(-v =verbose)使用,注意调试信息默认携带-v;logrotate -v -f /etc/logrotate.d/test.conf本例通过自定义配置文件来压缩指定日志文件来测试logrotate的使用。注意logrotate都是需要使用root来执行的,(但是可以通过配置项来指定生成的日志文件为普通用户的)。# 1. 生成一个日志文件man ps > test.logll -h test.log-rw-r--r-- 1 root root 54K Sep 6 11:36 test.log# 2. 编写对该日志文件如何处理的logrotate配置文件cat /etc/logrotate.d/test.conf/var/log/test.log {compressrotate 4size 30kcreate 0600 root root}# 3. 调试是否可以按照配置文件要求生成压缩文件logrotate -d /etc/logrotate.d/test.confreading config file /etc/logrotate.d/test.confreading config info for /var/log/test.logHandling 1 logsrotating pattern: /var/log/test.log 30720 bytes (4 rotations)empty log files are rotated, old logs are removedconsidering log /var/log/test.log log needs rotatingrotating log /var/log/test.log, log->rotateCount is 4dateext suffix '-20150906'glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'renaming /var/log/test.log.4.gz to /var/log/test.log.5.gz (rotatecount 4, logstart 1, i 4),renaming /var/log/test.log.3.gz to /var/log/test.log.4.gz (rotatecount 4, logstart 1, i 3),renaming /var/log/test.log.2.gz to /var/log/test.log.3.gz (rotatecount 4, logstart 1, i 2),renaming /var/log/test.log.1.gz to /var/log/test.log.2.gz (rotatecount 4, logstart 1, i 1),renaming /var/log/test.log.0.gz to /var/log/test.log.1.gz (rotatecount 4, logstart 1, i 0),renaming /var/log/test.log to /var/log/test.log.1creating new /var/log/test.log mode = 0600 uid = 0 gid = 0compressing log with: /bin/gzipremoving old log /var/log/test.log.5.gzerror: error opening /var/log/test.log.5.gz: No such file or directory# 4. 调试结果正常,实际测试下logrotate -f /etc/logrotate.d/test.confll -h test.log*-rw------- 1 root root 0 Sep 6 11:44 test.log-rw-r--r-- 1 root root 14K Sep 6 11:44 test.log.1.gz
rotating log /var/log/test.log, log->rotateCount is 4renaming /var/log/test.log.4.gz to /var/log/test.log.5.gz (rotatecount 4, logstart 1, i 4),renaming /var/log/test.log.3.gz to /var/log/test.log.4.gz (rotatecount 4, logstart 1, i 3),renaming /var/log/test.log.2.gz to /var/log/test.log.3.gz (rotatecount 4, logstart 1, i 2),renaming /var/log/test.log.1.gz to /var/log/test.log.2.gz (rotatecount 4, logstart 1, i 1),renaming /var/log/test.log.0.gz to /var/log/test.log.1.gz (rotatecount 4, logstart 1, i 0),renaming /var/log/test.log to /var/log/test.log.1emoving old log /var/log/test.log.5.gzerror: error opening /var/log/test.log.5.gz: No such file or directory
$ cat logrotate.conf /tmp/output.log { size 1k copytruncate create 700 bala bala rotate 4 compress }
$ cat logrotate.conf /tmp/output.log { size 1k copytruncate rotate 4 compress postrotate /home/bala/myscript.sh endscript }
$ cat logrotate.conf /tmp/output.log { size 1k copytruncate create compress compresscmd /bin/bzip2 compressext .bz2 rotate 4 }
linux一键安装web环境全攻略 在linux系统中怎么一键安装web环境方法
Linux网络基本网络配置方法介绍 如何配置Linux系统的网络方法
Linux下DNS服务器搭建详解 Linux下搭建DNS服务器和配置文件
对Linux进行详细的性能监控的方法 Linux 系统性能监控命令详解
linux系统root密码忘了怎么办 linux忘记root密码后找回密码的方法
Linux基本命令有哪些 Linux系统常用操作命令有哪些
Linux必学的网络操作命令 linux网络操作相关命令汇总
linux系统从入侵到提权的详细过程 linux入侵提权服务器方法技巧
linux系统怎么用命令切换用户登录 Linux切换用户的命令是什么
在linux中添加普通新用户登录 如何在Linux中添加一个新的用户
2012-07-10
CentOS 6.3安装(详细图解教程)
Linux怎么查看网卡驱动?Linux下查看网卡的驱动程序
centos修改主机名命令
Ubuntu或UbuntuKyKin14.04Unity桌面风格与Gnome桌面风格的切换
FEDORA 17中设置TIGERVNC远程访问
StartOS 5.0相关介绍,新型的Linux系统!
解决vSphere Client登录linux版vCenter失败
LINUX最新提权 Exploits Linux Kernel <= 2.6.37
nginx在网站中的7层转发功能