```bash
#!/bin/bash
#date:2021-08-02
#定义变量
LogrotateDir='/bankapp/cf/logs_logrotate'
PafaLog='/bankapp/deploy/work/logs/app/pafa.log'
PATH=$PATH
#打印当前时间
echo -e "\033[32m###############********当前时间:$(date +%F\ %T)********###############\033[0m"
#定义函数
NgConf() {
cat>$LogrotateDir/nginx<<EOF
$ServiceDir/*.log {
daily
rotate 7
compress
missingok
dateext
notifempty
sharedscripts
postrotate
if [ -e $ServiceDir/nginx.pid ];then
kill -USR1 \`cat $ServiceDir/nginx.pid\`
fi
endscript
}
EOF
}
TomConf() {
cat>$LogrotateDir/tomcat<<EOF
$ServiceDir/*.out
$ServiceDir/*.log
{
daily
rotate 7
missingok
compress
dateext
notifempty
copytruncate
}
EOF
}
WebConf() {
cat>$LogrotateDir/weblogic<<EOF
$ServiceDir/*.out
$ServiceDir/*.log
{
copytruncate
daily
rotate 7
dateext
missingok
compress
su wls81 log
}
/wls/applogs/rtlog/app/*.log {
copytruncate
daily
rotate 7
dateext
missingok
compress
su wls81 wls
}
EOF
}
PaConf() {
cat >$LogrotateDir/pafa<<EOF
/bankapp/deploy/work/logs/app/pafa.log {
copytruncate
daily
rotate 7
dateext
missingok
compress
}
EOF
}
PaClean() {
cat>/bankapp/cf/PaClean.sh<<EOF
#!/bin/bash
#定义变量
PATH=\$PATH
cd ~
#判断pafa日志文件
if [ -f \$Pafa_Log ];then
#clean polawork
#rm -rf /bankapp/deploy/work/logs/app/polawork/*
#删除7天前的pafa日志目录
find /bankapp/deploy/work/logs/app/ -name 'pafa-log*' -type d -mtime +7 -exec rm -rf {} \\;
#删除app目录下以pafa.log. 命名的日志文件
find /bankapp/deploy/work/logs/app/ -name 'pafa.log.*' -type f -mtime +7 -exec rm -f {} \\;
#压缩app目录下日志文件
find /bankapp/deploy/work/logs/app/ -name 'pafa.log.*' -type f -exec gzip {} \\;&>/dev/null
fi
EOF
}
FileChk() {
ls -lh $CheckFile |awk '{print $5}'|grep G
if [ $? -eq 0 ];then
#Size=`ls -lh $CheckFile |awk '{print $5}'|grep G`
Num=`ls -lh $CheckFile |awk '{print $5}'|grep G|sed s#G##g |awk '{print int($0)}'`
echo -e "\033[32m ${CheckFile}日志文件大小为:${Num}G\033[0m"
if [ $Num -gt 15 ];then
echo -e "\033[32m ${CheckFile}日志文件太大,需要手动清理!\033[0m"
cd $ServiceDir
echo -e "\033[32m要清空的日志文件为: ${CheckFile##*/}\033[0m"
>${CheckFile##*/}
if [ $? -eq 0 ];then
echo -e "\033[32m ${CheckFile}文件手动清空成功!\033[0m"
return 2
fi
else
return 0
fi
else
return 1
fi
}
ConfChk() {
if [ $? -eq 0 ];then
action "${Service}日志切割配置写入成功!" /bin/true
else
action "${Service}日志切割配置写入失败!" /bin/false
fi
}
CutChk() {
if [ $? -eq 0 ];then
action "${Service}日志切割成功!" /bin/true
else
action "${Service}日志切割失败!" /bin/false
fi
}
#编写定时任务
crontab -l|grep -w 'logs_logrotate.sh'
if [ $? -ne 0 ];then
echo "01 23 * * * /bin/bash /bankapp/cf/logs_logrotate.sh &>>/bankapp/cf/logrotate.log" >> /var/spool/cron/root
fi
#创建切割日志配置文件目录
[ -d $LogrotateDir ]||mkdir -p $LogrotateDir
#引用函数库
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
#判断nginx服务,写入日志切割配置,并切割日志
ps -ef|grep [n]ginx|grep -v 'auto' &>/dev/null
if [ $? -eq 0 ] ;then
Service="nginx"
ServiceDir="`/usr/sbin/lsof -p $(ps -ef|grep [n]ginx|grep -v grep|awk '/master/{print $2}')|grep log|awk '/access.log/{print $NF}'|head -1|sed -r 's#(.*)/.*#\1#g'`"
echo -e "\033[32m 服务目录为:$ServiceDir \033[0m"
if [ -d $ServiceDir ];then
#判断nginx日志切割配置,不存在则写入
if [ ! -f $LogrotateDir/nginx ];then
Ngconf
ConfChk
fi
#切割nginx日志
/usr/sbin/logrotate -f $LogrotateDir/nginx
CutChk
fi
fi
#判断tomcat服务
ps -ef |grep java|grep [t]omcat|grep -v 'auto' &>/dev/null
if [ $? -eq 0 ];then
Service="tomcat"
CheckFile=$(/usr/sbin/lsof -p $(ps -ef|grep java|grep [t]omcat|grep -v grep|awk '/catalina/{print $2}')|awk '/catalina.out/{print $NF}'|head -1)
echo -e "\033[32m 要切割的日志文件为:$CheckFile \033[0m"
if [ -f $CheckFile ];then
ServiceDir=${CheckFile%/*}
#echo "服务目录为:$ServiceDir"
#判断tomcat日志切割配置,不存在则写入
if [ ! -f $LogrotateDir/tomcat ];then
TomConf
ConfChk
fi
if [[ ${CheckFile##*/} == "catalina.out" ]];then
FileChk
if [ $? -ne 2 ];then
#切割tomcat日志
/usr/sbin/logrotate -f $LogrotateDir/tomcat
CutChk
fi
fi
fi
fi
#判断weblogic服务
ps -ef |grep java |grep 'weblogic.Server'|grep -v 'auto' &>/dev/null
if [ $? -eq 0 ];then
Service="weblogic"
CheckFile=/wls/applogs/rtlog/app/rcpm-common.log
if [ -f $CheckFile ];then
ServiceDir=${CheckFile%/*}
FileChk
fi
CheckFile=$(/usr/sbin/lsof -p $(ps -ef|grep [w]eblogic.Server|awk '/bloan/{print $2}')|awk '/out/{print $NF}'|head -1)
if [ -f $CheckFile ];then
ServiceDir=${CheckFile%/*}
echo -e "\033[32m服务目录为:$ServiceDir\033[0m"
echo -e "\033[32m要切割的日志文件为:$CheckFile\033[0m"
#判断weblogic日志切割配置,不存在则写入
if [ ! -f $LogrotateDir/weblogic ];then
WebConf
ConfChk
fi
FileChk
if [ $? -ne 2 ];then
#切割weblogic日志
/usr/sbin/logrotate -f $LogrotateDir/weblogic
CutChk
fi
fi
fi
#切割并压缩pafa日志
if [ -f $PafaLog ];then
Service="pafa"
#判断pafa日志切割配置,不存在则写入
if [ ! -f $LogrotateDir/pafa ];then
PaConf
ConfChk
fi
#写入脚本文件
if [ ! -f /bankapp/cf/PaClean.sh ];then
PaClean
if [ $? -eq 0 ];then
action "pafa清理脚本写入成功!" /bin/true
cd /bankapp/cf && chown bankdplyop.bankdplyop PaClean.sh
if [ $? -eq 0 ];then
action "脚本PaClean授权bankdplyop权限成功!" /bin/true
else
action "脚本PaClean授权bankdplyop权限失败!" /bin/false
fi
else
action "pafa清理脚本写入失败!" /bin/false
fi
fi
#切换到普通用户执行pafa日志清理脚本
CF=$(ls -dl /bankapp/cf|awk '{print $3}')
if [ $CF!="bankdplyop" ];then
chown bankdplyop.bankdplyop /bankapp/cf
sudo -u bankdplyop /bin/bash /bankapp/cf/PaClean.sh
if [ $? -eq 0 ];then
action "pafa日志手动清理成功!" /bin/true
else
action "pafa日志手动清理失败!" /bin/false
fi
fi
ServiceDir=/bankapp/deploy/work/logs/app
CheckFile=$PafaLog
echo -e "\033[32m要切割的日志文件为:$CheckFile\033[0m"
if [[ ${CheckFile##*/} == "pafa.log" ]];then
FileChk
if [ $? -ne 2 ];then
#切割pafa日志
/usr/sbin/logrotate -f $LogrotateDir/pafa
CutChk
fi
fi
fi
```
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现