生产环境WEB服务管理脚本之重启脚本

环境说明:

    公司是做在线教育的互联网企业,WEB架构为:前端使用LVS + Heartbeat做负载均衡,后端主要是Apache/Nginx + Tomcat,缓存有redis和Memcached,数据库使用的Oracle和Mysql。 

脚本实现目的:

    通过单个脚本重启系统所有WEB应用,并能根据参数自动重启相应的WEB应用

脚本思路:

    所有的WEB应用都是通过配置文件的方式传递给脚本,以方便批量部署。

脚本内容:

#!/bin/bash
#This shell-script is use for restart httpd and tomcat
#Created in 2012-11-01
#Last changed in 2013-05-28
source ~/.bash_profile &>/dev/null

dir=''
dir=/tol/app
dir2=/tol/script
cd $dir2

dt2=`date +"%Y-%m-%d"`
ls $dir2/logs &>/dev/null || mkdir -p $dir2/logs &>/dev/null
log="$dir2/logs/tomcat-restart-$dt2.log"
host=`/sbin/ifconfig |grep "inet addr"|cut -d ':' -f2 |awk '{print $1}'|head -1`
conf=$dir2/tomcat-restart.conf
conf2=$dir2/tomcatwget.conf
conf3=$dir2/web.conf
sh_name=$0

function shijian () {
	dt=`date +"%Y-%m-%d-%H:%M:%S"`
}
function apache_restart () {
#stop
	user=`ps  -elf |grep -v grep|grep -v root|grep '/bin/httpd'|grep "$dir"|awk '{print $3}'|head -1`
	$dir/apache/bin/httpd -k stop  &>/dev/null
	sleep 5
	opid=''	
	opid=`ps -elf |grep -v grep|grep "httpd"|grep "$dir"|awk '{print $4}'`
	if test -n "$opid" ; then
		kill -9  $opid
	fi
#start
	/usr/bin/ipcs -s | grep "$user"| gawk '{ print $2 }' | xargs -n 1 ipcrm sem &> /dev/null
	$dir/apache/bin/httpd -k start  &>/dev/null
	npid=`ps -elf |grep -v grep|grep "httpd"|grep root|grep "$dir" |awk '{print $4}'`
	shijian
	if test -z "$npid" ; then
		echo "The $dt $host apache restart fail by $sh_name" >> $log
		echo "The $dt $host apache restart fail by $sh_name" |mail -s "check $host apache restart fail" jiank
	else
		echo "The $dt $host apache is restart,the new pid=$npid" >> $log
	fi
}
function nginx_restart () {
#stop
	opid=''
	opid=`cat $dir/nginx/logs/nginx.pid`
	kill -QUIT $opid
	sleep 3
	opid=`ps -elf |grep -v grep|grep process|grep nginx |awk '{print $4}'`
	if test ! -z "$opid" ; then
        	kill -9 $opid
	fi
#start
	$dir/nginx/sbin/nginx -c $dir/nginx/conf/nginx.conf
	/bin/chown -R nginx.nginx $dir/nginx/*
	npid=''
	npid=`ps -elf |grep -v grep|grep process|grep nginx |awk '{print $4}'`
	shijian
	if test -z "$npid" ; then
        	echo "The $dt $host nginx restart fail by $sh_name" >> $log
        	echo "The $dt $host nginx restart fail by $sh_name" |mail -s "check $host nginx restart fail" jiank
	else
		echo "The $dt $host nginx is restart,the new pid=$npid" >> $log
	fi
}
function http_restart () {
        if test ! -f $conf3 ; then
                echo "The $conf3 is not exist" >> $log
                apache_restart
                sleep 3
                nginx_restart
        else
                while read line
                do
                dir=`echo $line |awk -F\; '{print $1}'`
                softname=`echo $line |awk -F\; '{print $2}'`
                $softname\_restart
                done < $conf3
        fi
}

#conf-check
if test ! -f $conf ; then
	echo "The $conf is not exist"
	exit 0
fi
shijian
echo "$dt" >> $log
echo "$host" >> $log

case $1 in
	"")
		http_restart
		;;
	"all")
		http_restart
		;;
	"apache")
		apache_restart; exit
		;;
	"nginx")
		nginx_restart; exit
		;;
	"$1")
                option=`cat $conf |grep "\<$1\>"|grep -v '^\s*#'`
                if test ! -z "$option" ; then
                        echo "$option" > $dir2/$1-restart.conf
                        conf=$dir2/$1-restart.conf
                else
                        echo "Usage $0 {all|apache|TOMCAT_NAME}"
                        exit
                fi
		;;
esac

#tomcat-restart
while read line
do
 kg=`echo "$line" |grep '^\s*#'`

 if [ "$line" = "" ]; then
        continue
 elif test "$kg" ; then
        continue
 fi

shijian
name=`echo $line |awk -F\; '{print $1}'`
function pid () {
        pid=''
        pid=`ps -elf|grep java|grep "$dir"|grep $name|awk '{print $4}'`
}
function down () {
	`echo $line |awk -F\; '{print $2}'`
	sleep 3
	pid
	if test -n "$pid" ; then
 		kill -9 $pid
 	fi
 	rm -rf $dir/$name/work/Catalina/*
 	rm -rf $dir/$name/temp/*
}
function up () {
	`echo $line |awk -F\; '{print $3}'`
}

down
up
pid
shijian
if test -n "$pid" ; then
  echo "$dt Restart $name success pid= " $pid >> $log
else
  up
  echo "The $dt $host $name restart fail by $sh_name" >> $log
  echo "The $dt $host $name restart fail by $sh_name" |mail -s "check $host $name restart fail" jiank
 fi
done < $conf

#backup-*.xml
if test -f "$conf2" ; then
	while read line2
	do
	shijian
	xml1=`echo $line2 |awk -F\; '{print $2}'`
	xml2=`echo $line2 |awk -F\; '{print $3}'`
		if test -n "$xml1" ; then
			\cp $xml1 $xml1.bak && echo "$dt backup $xml1 is ok" >> $log
		fi
		if test -n "$xml2" ; then	
			\cp $xml2 $xml2.bak && echo "$dt backup $xml2 is ok" >> $log
		fi
	done < $conf2
fi

脚本配置文件:

tomcata;/etc/init.d/tomcata stop;/etc/init.d/tomcata start;

tomcatb;/etc/init.d/tomcatb stop;/etc/init.d/tomcatb start;

你可能感兴趣的:(linux,Web,shell脚本)