nginx切割日志部署脚本编写

#!/bin/bash
# utf-8
# description: 部署nginx_lograte.sh脚本
# ---------------------------------------------------------------------
script_name="logrotate_new.sh"
script_download_directory="http://172.20.147.61/CentOS/app/script/hby"     # 脚本下载路径
script_directory="/export/servers/nginx/sbin"                              # 脚本存放路径
crontab_file_location="/etc/crontab"                                       # crontab文件路径
old_crontab_expression_keyword1="/export/servers/nginx/sbin/logrotate.sh"   # 旧的crontab表达式
old_crontab_expression_keyword2="/export/servers/nginx/sbin;./logrotate.sh"   # 旧的crontab表达式
# ---------------------------------------------------------------------

# 部署切割脚本
deploy_script() {
 echo "部署切割脚本  [开始]"
 local download_url="${script_download_directory}/${script_name}"
 local script_location="${script_directory}/${script_name}"

 if /usr/bin/wget -q -O ${script_location} ${download_url}; then
     chmod +x "${script_location}"
     echo "部署切割脚本  [完毕]"
 else
     echo "部署切割脚本 [失败]"
     exit 1
 fi

}

# 部署crontab
deploy_crontab() {
 local script_location="${script_directory}/${script_name}"
 local crontab_expression="18 * * * * root sh ${script_location}"
 # 注释掉之前的crontab
 echo "部署crontab  [开始]"
 # 添加新的crontab
 ## 判断之前是否有新脚本且没注释
 local count=$(grep -v "#" "${crontab_file_location}" | grep -c "${script_location}")
 if (( count == 0 )); then
   /bin/sed -i "s@^[^#].*${old_crontab_expression_keyword1}*@#&@g" "${crontab_file_location}"
   /bin/sed -i "s@^[^#].*${old_crontab_expression_keyword2}*@#&@g" "${crontab_file_location}"
   echo >> /etc/crontab
   echo "# nginx new version log cutting" >> /etc/crontab
   echo "${crontab_expression}" >> "${crontab_file_location}"
   echo "部署crontab  [完毕]"
 else
   echo "部署crontab  [已存在]"
 fi

}

# 主函数
main() {
 deploy_script
 deploy_crontab
}

main
posted @   SpecialSpeculator  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
历史上的今天:
2021-12-27 Calendar设置周一为一周的第一天
点击右上角即可分享
微信分享提示