Nginx(十七) 日志轮询/切割

1.编写shell脚本

复制代码
Nginx_Log_Path="/usr/local/nginx/logs/"
Dateformat=$(date -d "yesterday" +%Y%m%d)
mv ${Nginx_Log_Path}/access.log ${Nginx_Log_Path}/access-${Dateformat}.log
mv ${Nginx_Log_Path}/access_8688.log ${Nginx_Log_Path}/access_8688-${Dateformat}.log
mv ${Nginx_Log_Path}/error.log ${Nginx_Log_Path}/error-${Dateformat}.log
/usr/local/nginx/sbin/nginx -s reopen

可以直接用win系统的文本编辑器来新建.sh。文件重命名为split_nginx_log.sh,并上传到服务器。

2.文件授权

复制代码
[root@reader sbin]# chmod +x split_nginx_log.sh

3.手动执行脚本进行测试

复制代码
[root@reader sbin]# pwd
/usr/local/nginx/sbin
[root@reader sbin]# ll
total 24836
-rwxr-xr-x 1 root root 7050624 Dec 30 20:15 nginx
-rwxr-xr-x 1 root root 6148992 Dec  8 20:30 nginx_20231208.bak
-rwxr-xr-x 1 root root 6066592 Nov 26 21:32 nginx_20231230.bak
-rwxr-xr-x 1 root root 6148992 Dec 30 20:01 nginx.bak
-rwxr-xr-x 1 root root     716 Jan  4 22:44 split_nginx_log.sh
[root@reader sbin]# split_nginx_log.sh
[root@reader sbin]# 

如果执行脚本后提示以下错误

复制代码
-bash: /usr/local/nginx/sbin/split_nginx_log.sh: /usr/bin/bash^M: bad interpreter: No such file or directory

说明文件格式不支持,因为windows系统的文件格式是dos,而linux系统是unix,所以需要将文件格式从dos修改为unix。

复制代码
# 打开vim
vim split_nginx_log.sh
# 使用set修改ff
:set ff=unix
# 保存退出
:wq

4.使用定时任务开启脚本

复制代码
[root@reader sbin]# crontab -e

00 00 * * * /bin/sh /usr/local/nginx/sbin/split_nginx_log.sh

### 执行:wq保存文件

# 重启定时任务
/bin/systemctl restart crond.service
相关推荐
浅时光_c18 小时前
3 shell脚本编程
linux·开发语言·bash
Lucis__19 小时前
一文读懂TCP通信机制:基于相关API构建可靠性连接
linux·网络·tcp/ip
_深海凉_19 小时前
LeetCode热题100-有效的括号
linux·算法·leetcode
鹿鸣天涯20 小时前
Xftp传输文件时,解决“无法显示远程文件夹”方法
运维·服务器·计算机
unDl IONA21 小时前
服务器部署,用 nginx 部署后页面刷新 404 问题,宝塔面板修改(修改 nginx.conf 配置文件)
运维·服务器·nginx
零号全栈寒江独钓21 小时前
基于c/c++实现linux/windows跨平台获取ntp网络时间戳
linux·c语言·c++·windows
Web极客码21 小时前
WordPress管理员角色详解及注意事项
运维·服务器·wordpress
左手厨刀右手茼蒿1 天前
Linux 内核中的进程管理:从创建到终止
linux·嵌入式·系统内核
geinvse_seg1 天前
中小团队如何低成本搭建项目管理系统?基于 Ubuntu 的 Dootask 私有化部署实战
linux·运维·ubuntu
CSCN新手听安1 天前
【linux】高级IO,以ET模式运行的epoll版本的TCP服务器实现reactor反应堆
linux·运维·服务器·c++·高级io·epoll·reactor反应堆