您的位置:首页 > 运维架构 > Shell

我的工作脚本

2016-03-21 11:10 309 查看
#!/bin/bash
#The script for auto confgure web server.
#The script by zhang hk.
#Date 2016.3.16
. /etc/init.d/functions
NginxDir=/usr/local/nginx
TomDir=/home/ibumobile

#Remote Slave Server
LinkSlave () {
SlaveServer=192.168.137.11
SlavePort=24
/usr/bin/ssh -p $SlavePort root@"$SlaveServer" "$1"
}

ReMasterNginxConf (){
#Confgure Master Web Server and reload confgure.
cd $NginxDir/conf
$NginxDir/sbin/nginx -c $NginxDir/conf/extra/$1.conf -t &>/tmp/1.log
if [ $(grep "ok" /tmp/1.log|wc -l) -eq 1 ] ; then
\cp extra/$1.conf ./nginx.conf && /usr/local/nginx/sbin/nginx -s reload
if [ $? -eq 0 ]
then
echo "Master Configure Reload ..... OK."
else
exit 1
fi
else
echo "Master Nginx Configure File Erro."
exit 1
fi
}

ReSlaveNginxConf () {
#Confgure Slave Web Server and reload confgure
LinkSlave "$NginxDir/sbin/nginx -c $NginxDir/conf/extra/$1.conf -t" &>/tmp/1.log
if [ $(grep "ok" /tmp/1.log|wc -l) -eq 1 ] ; then
LinkSlave "cd $NginxDir/conf &&  \cp extra/$1.conf ./nginx.conf && /usr/local/nginx/sbin/nginx -s reload"
if [ $? -eq 0 ]
then
echo "Slave Configure Reload is ..... OK."
else
exit 1
fi
else
echo "Slave Nginx Configure File Erro."
exit 1
fi
}

JavaPid () {
cd $TomDir/$1
if [ -f "$2.pid"  ]
then
kill -9 $(cat ./$2.pid) && rm -f ./$2.pid
[ $? -eq 0 ] &&  mv $TomDir/$1/logs/catalina.out $TomDir/$1/logs/catalina.$(date +%F-%T).log
else
echo "Not found PID file."
exit 1
fi

[ $? -eq 0 ] && $TomDir/$1/bin/startup.sh >/dev/null
}

GrepLog (){
for ((i=1;i<100;i++))
do
if [ $(grep 'Server startup' $TomDir/$1/logs/catalina.out|wc -l) -eq 1 ]
then
echo "$1_$2 Server startup ..... OK"
break
else
sleep 1s
continue
fi
done
}

###########################################################################################################
#First Reload Master and Slave Nginx Configure.
ReMasterNginxConf nginxOne
ReSlaveNginxConf nginxOne
##########################################################################################################
#Stop Slave JAVA Port 8102.
LinkSlave  "if [ -f $TomDir/tomcat_five/8102.pid ] ; then cat $TomDir/tomcat_five/8102.pid|xargs kill -9 && rm -f $TomDir/tomcat_five/8102.pid && mv $TomDir/tomcat_five/logs/catalina.out $TomDir/tomcat_five/logs/catalina.$(date +%F-%T).log && $TomDir/tomcat_five/bin/startup.sh;else echo 'Not found PID file.' && exit 1 ; fi "

for ((i=1;i<100;i++))
do
if [ $(LinkSlave "grep 'Server startup' $TomDir/tomcat_five/logs/catalina.out|wc -l") -eq 1 ]
then
echo "Tomcat_five_8102 Server startup ..... OK"
break
else
sleep 1s
continue
fi
done
#############################################################################################################
#Stop Master Java.
JavaPid tomcat6_four 8102
if [ $? -eq 0 ]
then
GrepLog tomcat6_four 8102
fi

###########################################################################################################
#Second Reload Master and Slave Nginx Configure.
ReMasterNginxConf nginxTwo
ReSlaveNginxConf nginxTwo

##########################################################################################################
#Stop Slave JAVA Port 8088.
LinkSlave  "if [ -f $TomDir/tomcat6/8088.pid ] ; then cat $TomDir/tomcat6/8088.pid|xargs kill -9 && rm -f $TomDir/tomcat6/8088.pid && mv $TomDir/tomcat6/logs/catalina.out $TomDir/tomcat6/logs/catalina.$(date +%F-%T).log && $TomDir/tomcat6/bin/startup.sh;else echo 'Not found PID file.' && exit 1 ; fi "

for ((i=1;i<100;i++))
do
if [ $(LinkSlave "grep 'Server startup' $TomDir/tomcat6/logs/catalina.out|wc -l") -eq 1 ]
then
echo "Tomcat6_8088 Server startup ..... OK"
break
else
sleep 1s
continue
fi
done

#############################################################################################################
#Stop Master Java.
JavaPid tomcat6_main 8088
if [ $? -eq 0 ]
then
GrepLog tomcat6_main 8088
fi

JavaPid tomcat6_third 8099
if [ $? -eq 0 ]
then
GrepLog tomcat6_third 8099
fi

###########################################################################################################
#First Reload Master and Slave Nginx Configure.
ReMasterNginxConf nginx
ReSlaveNginxConf nginx

exit 0
#The script end.
#############################################################################################################
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell