0%

shell脚本

shell

  • Tomcat
  • Nginx

Tomcat 监控重启

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/bin/bash

# 这里指明Tomcat版本
export JAVA_HOME=/local/server/1.6.0_38
export CLASSPATH=$JAVA_HOME/lib/
export PATH=$PATH:$JAVA_HOME/bin
export PATH JAVA_HOME CLASSPATH

# tomcat所在目录
tomcat_path1=/local/server/tomcat_UserLoginSys_7771
tomcat_path2=/local/server/tomcat_wx_manage_18050
tomcat_path3=/local/server/tomcat_InstantClient_7781
tomcat_path4=/local/server/tomcat_weixinzhcw_8040
tomcat_path5=/local/server/tomcat_zhcwwxweb_8030

# jar所在目录
# jar_PATH=/local/webapp/

date=$(date +%Y-%m-%d-%k:%M:%S)

# 查询程序占用
tomcat_UserLoginSys_7771=`ps -ef|grep tomcat_UserLoginSys_7771 |grep -v grep|wc -l`
tomcat_wx_manage_18050=`ps -ef|grep tomcat_wx_manage_18050 |grep -v grep|wc -l`
tomcat_InstantClient_7781=`ps -ef|grep tomcat_InstantClient_7781 |grep -v grep|wc -l`
tomcat_weixinzhcw_8040=`ps -ef|grep tomcat_weixinzhcw_8040 |grep -v grep|wc -l`
tomcat_zhcwwxweb_8030=`ps -ef|grep tomcat_zhcwwxweb_8030 |grep -v grep|wc -l`


if [ $tomcat_UserLoginSys_7771 -eq 0 ]
then
nohup ${tomcat_path1}/bin/startup.sh > /dev/null 2>&1 &
echo "${date} tomcat_UserLoginSys_7771已重启" >> /local/script/restart.log
fi

if [ $tomcat_wx_manage_18050 -eq 0 ]
then
nohup ${tomcat_path2}/bin/startup.sh > /dev/null 2>&1 &
echo "${date} tomcat_wx_manage_18050已重启" >> /local/script/restart.log
fi

if [ $tomcat_InstantClient_7781 -eq 0 ]
then
nohup ${tomcat_path3}/bin/startup.sh > /dev/null 2>&1 &
echo "${date} tomcat_InstantClient_7781已重启" >> /local/script/restart.log
fi

if [ $tomcat_weixinzhcw_8040 -eq 0 ]
then
nohup ${tomcat_path4}/bin/startup.sh > /dev/null 2>&1 &
echo "${date} tomcat_weixinzhcw_8040已重启" >> /local/script/restart.log
fi

if [ $tomcat_zhcwwxweb_8030 -eq 0 ]
then
nohup ${tomcat_path5}/bin/startup.sh > /dev/null 2>&1 &
echo "${date} tomcat_zhcwwxweb_8030已重启" >> /local/script/restart.log
fi

# */1 * * * * /local/script/restart.sh > /dev/null 2>&1

Nginx日志打包压缩

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
#Nginx日志文件所在目录
LOG_PATH=/local/server/nginx/logs/

#获取昨天的日期
YESTERDAY=$(date -d "1 days ago" +%Y-%m-%d)

#获取pid文件路径
PID=/local/server/nginx/nginx.pid

#匹配归档各access文件
mkdir -p ${LOG_PATH}accesslog/${YESTERDAY}
for file in `ls ${LOG_PATH} | grep access.log`
do
mv ${LOG_PATH}$file ${LOG_PATH}accesslog/${YESTERDAY}/$file
done

# 打包
cd /local/server/nginx/logs/accesslog/$(date -d "1 days ago" +%Y-%m-%d)/ && ls | awk '{ print "tar zcvf "$0".tar.gz " $0|"/bin/bash" }' && cd
cd /local/server/nginx/logs/accesslog/$(date -d "1 days ago" +%Y-%m-%d)/ && ls *.log | xargs rm -f && cd

#向Nginx主进程发送USR1信号,重新打开日志文件
kill -USR1 `cat ${PID}`

#保留180天的文件
cd /local/server/nginx/logs/accesslog/ && ls -t |awk 'NR>180' |xargs rm -rf && cd


----------- 本文结束 -----------




Buy me a coffee.