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 @ 2023-12-27 14:45  SpecialSpeculator  阅读(7)  评论(0编辑  收藏  举报