关于logrotate的额外补充

https://rorschachchan.github.io/2018/02/12/日志文件管理者:Logrotate/ 里面已经简单介绍了logrotate命令,这里还有一些额外补充的东西:

1)查看logrotate对log文件的具体执行情况的语句是cat /var/lib/logrotate.status,效果如图:
paradin

2)使用-v-d参数时,显示log does not need rotating,这是因为logrotate在对status未记录的文件进行转储时,会在status添加一条该文件的记录,并将操作时间设为当天。之后程序再次对此文件进行转储时发现这个文件今天已经操作过,就不再进行相关操作。要是想解决这个问题可以使用-s指定logrotate状态文件;

3)分割日志时报错:error: skipping "/var/log/nginx/test.access.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.这是当前用户不是root,需要添加su root list这个语句到对应的logrotate配置文件里,比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/var/log/nginx/*.log {
su root list #第一句添加
daily
missingok
rotate 52
compress
delaycompress
notifempty
#ifempty
create 0640 www-data adm
sharedscripts
postrotate
[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
endscript
}

4)如果觉得使用logrotate很麻烦,而当某个文件过大的时候,要实现把该文件压缩并且拆成若干个指定大小的文件,怎么办?

1
tar -zcvf 新文件名.tar.gz 原文件名 | split -b 每个分格包大小 -d -a 1 - 新文件名.tar.gz

比如:tar -zcvf ABC.tar.gz ABC | split -b 4000M -d -a 1 - ABC.tar.gz。这个命令就是把ABC这个文件压缩成ABC.tar.gz,但是如果ABC大于4000M就会切块,切成ABC.tar.gz.0,ABC.tar.gz.1,ABC.tar.gz.2……这个样子。

1
2
3
//使用split命令,-b 4000M 表示设置每个分割包的大小,单位还是可以k
// -d 参数指定生成的分割包后缀为数字的形式
//-a x来设定序列的长度(默认值是2),这里设定序列的长度为1

如果要把这一堆已经切块的文件重新接压缩的命令:cat ABC.tar.gz.* | tar -zxv;

5)如果用kill -HUP来重启一个包含守护进程的进程,比如httpd,一条语句搞定:

1
ps -ef | grep httpd | grep -v grep | awk '{ print $2; }' | xargs -L 1 sudo kill -HUP

这里面首先用awk获取到httpd的pid进程号,然后把这个进程号传给了xargs,通过-L 1来一次提取一行pid值,然后分批进行kill -HUP;如果想要更改配置而不需停止并重新启动服务,请使用kill -HUP。在对配置文件作必要的更改后,发出该命令以动态更新服务配置。

6)想更多的了解守护进程,参看http://www.cnblogs.com/mickole/p/3188321.html

paradin

感谢您请我喝咖啡~O(∩_∩)O,如果要联系请直接发我邮箱chenx1242@163.com,我会回复你的
-------------本文结束感谢您的阅读-------------