网站首页 > 技术教程 正文
编辑 /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily # 每日轮转
missingok
rotate 14 # 保留14天的数据
dateext # 支持日期格式名称的切割方式
dateformat -%Y%m%d # 定义文件名称
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
# 告诉 nginx 重新打开日志文件,以便实现日志切割,默认的语句不生效
nginx -s reopen >/dev/null 2>&1
# 自定义日志文件备份
cur_date=$(date +%Y%m%d)
if [ ! -d /data/backup/nginx ];then
mkdir -p /data/backup/nginx
fi
cp -f /var/log/nginx/*-$cur_date /data/backup/nginx
endscript
}
为什么要编辑这个文件?
- 按需定义功能,如定义文件的名称、保留的天数、轮转的时机,是否压缩以及备份等功能
- 原来的轮转脚本运行后,日志不会写入access.log
手动触发日志轮转
sudo logrotate -f /etc/logrotate.conf
任务轮转的时机
触发机制定义在这里 /etc/crontab
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
- 第一行 (17 * * * * root cd / && run-parts --report /etc/cron.hourly) 表示每小时的第 17 分钟执行 /etc/cron.hourly 目录中的所有脚本。
- 第二行 (25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )) 表示每天的第 6 小时 25 分钟执行 /etc/cron.daily 目录中的所有脚本。
- 第三行 (47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )) 表示每周的第 7 天(星期天)的第 6 小时 47 分钟执行 /etc/cron.weekly 目录中的所有脚本。
- 第四行 (52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )) 表示每月的第 1 天的第 6 小时 52 分钟执行 /etc/cron.monthly 目录中的所有脚本。
这些行的各个字段解释如下:
- 第一个字段:分钟 (0 - 59)
- 第二个字段:小时 (0 - 23)
- 第三个字段:月份中的某一天 (1 - 31)
- 第四个字段:月份 (1 - 12) 或者缩写的月份名称
- 第五个字段:星期中的某一天 (0 - 6),星期天可以是 0 或 7,或者缩写的星期名称
- user-name:指定了使用哪个用户执行命令
- command to be executed:要执行的命令
因此,你可以根据这个格式查看系统中定期运行的 cron 任务。如果你想了解更多关于 cron 任务的信息,可以查看 /etc/cron.d/ 和 /etc/crontab 等文件,以及 /etc/cron.daily/、/etc/cron.weekly/ 和 /etc/cron.monthly/ 目录。
猜你喜欢
- 2024-10-11 CentOS中Nginx日志自动分割及日志分析
- 2024-09-21 滚动 docker 中的 nginx 日志,竟然如此简单
- 2024-09-21 Nginx Access Log日志统计分析常用命令
- 2024-09-21 GoAccess - 实时 Apache 和 Nginx 日志分析工具
- 2024-09-21 滚动 docker 中的 nginx 日志思路详解
- 2024-09-21 一项一项教你测等保2.0——Nginx中间件
- 2024-09-21 Nginx 日志文件详解:监控与诊断利器
- 2024-09-21 centos7系统nginx的 日志清理(nginx关闭日志记录)
- 2024-09-21 nginx入门——nginx访问日志(六)(nginx访问日志格式)
- 2024-09-21 通过filebeat、logstash、rsyslog 几种方式采集 nginx 日志
你 发表评论:
欢迎- 最近发表
-
- Linux新手必看:几种方法帮你查看CPU核心数量
- linux基础命令之lscpu命令(linux中ls命令的用法)
- Linux lscpu 命令使用详解(linux常用ls命令)
- 如何查询 Linux 中 CPU 的数量?这几个命令要知道!
- 在linux上怎么查看cpu信息(linux如何查看cpu信息)
- 查看 CPU 的命令和磁盘 IO 的命令
- 如何在CentOS7上改变网卡名(centos怎么改网卡名字)
- 网工必备Linux网络管理命令(网工必备linux网络管理命令是什么)
- Linux 网络命令知多少(linux 网络 命令)
- Linux通过命令行连接wifi的方式(linux命令行连接无线网)
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)
本文暂时没有评论,来添加一个吧(●'◡'●)