【Nginx】日志切割和定期删除日志

本文日志切割和定期删除日志都是通过定时任务(cron)执行脚本完成

日志切割脚本

cut_nginx_log_of_yesterday.sh

#!/bin/bash
# program:
#       cut nginx log of yesterday
source /etc/profile

log_path="/data/logs/nginx/"
yesterday=$(date -d "yesterday" +"%Y%m%d")

mv ${log_path}error.log ${log_path}error.$yesterday.log
mv ${log_path}pc_access.log ${log_path}pc_access.$yesterday.log

nginx -s reload

定期删除脚本

rm_nginx_log.sh

#!/bin/bash
#program:
#       delete nginx logs of 10 days ago
find /data/logs/nginx/ -mtime +10 -name "*.log" -exec rm -rf {} \;

cron 配置文件

cron.conf 配置以上两个脚本到定时任务

0 0 * * * /bin/bash /opt/script/cut_nginx_log/cut_nginx_log_of_yesterday.sh
0 16 * * * /bin/bash /opt/script/cut_nginx_log/rm_nginx_log.sh

脚本上传Nginx服务器并添加定时任务

本地编写脚本add.sh,把cut_nginx_log_of_yesterday.sh、rm_nginx_log.sh和cron.conf上传到Nginx服务器,并添加定时任务

#!/bin/bash
#program:
#       add cron to cut nginx log on nginx server

ip_array=(192.168.1.1 192.168.1.2 192.168.1.3)

cron_conf_from_path="/data/scripts/cron_to_cut_nginx/"
cron_script_from_path="/data/scripts/cron_to_cut_nginx/"

cron_conf_to_path="/opt/conf/cron_to_cut_nginx_log/"
cron_script_to_path="/opt/script/cut_nginx_log/"

for ip in ${ip_array[@]}
do
        ssh root@${ip} "mkdir -p ${cron_conf_to_path}"
        scp ${cron_conf_from_path}cron.conf root@${ip}:${cron_conf_to_path}cron.conf

        ssh root@${ip} "mkdir -p ${cron_script_to_path}"
        scp ${cron_script_from_path}cut_nginx_log_of_yesterday.sh root@${ip}:${cron_script_to_path}cut_nginx_log_of_yesterday.sh
        scp ${cron_script_from_path}rm_nginx_log.sh root@${ip}:${cron_script_to_path}rm_nginx_log.sh
        ssh root@${ip} "crontab ${cron_conf_to_path}cron.conf"
        echo "${ip}"
        ssh root@${ip} "crontab -l"

done

最后本地执行 sh add.sh就把前面的脚本上传到了nginx服务器上,并且加入了cron定时任务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值