weblogic的shell管理脚本

Weblogic 脚本管理

该脚本用于管理weblogic ,涉及到启动,重启,停止,批量启动,批量重启,查看状态,查看端口等。

使用的命令介绍:

weblogic -help 来查看,有哪些使用参数。

参考脚本如下:

#!/bin/sh
fir=$1
instance=$2
a=(" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ")
b=(" "" "" "" "" "" "" "" ")
c=(" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ")
d=(" "" "" "" "" "" ")
WEBLOGIC_HOME=/opt/oracle/bea/user_projects/domains
RETVAL=0

sprint_status()
{
echo
echo "Weblogic Processes in Instance: `hostname`"
echo "------------------+----------------------------+---------+-----------"
echo "Domain | intances | state | pid "
echo "------------------+----------------------------+---------+-----------"
for i in `ls -l $WEBLOGIC_HOME| awk '/^d/{ print $NF}'`
do
fri_str=`echo $i | awk '{print length($1)}'`
for y in `ls -l $WEBLOGIC_HOME/$i/servers 2>&1|awk '/^d/{ print $NF}'`
do
sd_str=`echo $y | awk '{print length($1)}'`
ps aux | grep -w $y | grep -v grep | grep -vw "bash -c" | grep -vw "sh" > /dev/null
ret=$?
if [ $ret -eq 0 ]; then
echo "$i${a:$fri_str}| $y${c:$sd_str}| Alive | `ps aux | grep -w $y |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$y[^-]"| awk '{print $2}'` "
else
echo "$i${a:$fri_str}| $y${c:$sd_str}| Down | N/A "
fi
RETVAL=$?
done
done
echo
}

sprint_status_port()
{
echo
echo "Weblogic Processes in Instance: `hostname`"
echo "------------------+----------------------------+---------+-------+----------"
echo "Domain | intances | state | prot | pid "
echo "------------------+----------------------------+---------+-------+----------"
for i in `ls -l $WEBLOGIC_HOME| awk '/^d/{ print $NF}'`
do
fri_str=`echo $i | awk '{print length($1)}'`
for y in `ls -l $WEBLOGIC_HOME/$i/servers 2>&1|awk '/^d/{ print $NF}'`
do
port=`cat $WEBLOGIC_HOME/$i/config/config.xml | grep -A9 "<name>$y</name>"`
nport=`echo $port|awk -F "<listen-port>" '{print $2}' | awk -F "</listen-port>" '{print $1}'`
str_nport=`echo $nport | awk '{print length($1)}'`
sd_str=`echo $y | awk '{print length($1)}'`
if [ $str_nport -eq 0 ];then
ps aux | grep -w $y | grep -v grep | grep -vw "bash -c"|grep -vw "sh" > /dev/null
ret=$?
if [ $ret -eq 0 ]; then
echo "$i${a:$fri_str}| $y${c:$sd_str}| Alive | N/A | `ps aux | grep -w $y |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$y[^-]"| awk '{print $2}'` "
else
echo "$i${a:$fri_str}| $y${c:$sd_str}| Down | N/A | N/A "
fi
RETVAL=$?
else
ps aux | grep -w $y | grep -v grep | grep -vw "bash -c" > /dev/null
ret=$?
if [ $ret -eq 0 ]; then
echo "$i${a:$fri_str}| $y${c:$sd_str}| Alive | $nport | `ps aux | grep -w $y |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$y[^-]"| awk '{print $2}'` "
else
echo "$i${a:$fri_str}| $y${c:$sd_str}| Down | $nport | N/A "
fi
RETVAL=$?
fi
done
done
echo
}

sprint_status_app()
{
echo
echo "Weblogic Processes in Instance: `hostname`"
echo "------------------+----------------------------+-----------+---------------------+----------------------------------"
echo "Domain | intances | state | pid | applications "
echo "------------------+----------------------------+-----------+---------------------+----------------------------------"
for i in `ls -l $WEBLOGIC_HOME| awk '/^d/{ print $NF}'`
do
fri_str=`echo $i | awk '{print length($1)}'`

for y in `ls -l $WEBLOGIC_HOME/$i/servers 2>&1|awk '/^d/{ print $NF}'`
do
sd_str=`echo $y | awk '{print length($1)}'`
fir_pid_str=`ps aux | grep -w $y |grep -v "grep"| awk 'NR==1{print $2}'| awk '{print length($1)}'`
sd_pid_str=`ps aux | grep -w $y |grep -v "grep"| awk 'NR==2{print $2}'| awk '{print length($1)}'`
if [ -e $WEBLOGIC_HOME/$i/servers/$y/stage ];then
for z in `ls -l $WEBLOGIC_HOME/$i/servers/$y/stage 2>&1| awk '/^d/{ print $NF}'`
do
ps aux | grep -w $y | grep -v grep | grep -vw "bash -c"|grep -vw "sh" > /dev/null
ret=$?
if [ $ret -eq 0 ]; then
echo "$i${a:$fri_str}| $y${c:$sd_str}| started | sh:`ps aux | grep -w $y |grep -v "grep"|grep -w "startManagedWebLogic.sh"|grep "$y[^-]"|awk '{print $2}'`${d:$fir_pid_str}java:`ps aux | grep -w $y |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$y[^-]"| awk '{print $2}'`${d:sd_pid_str}| $z "
else
echo "$i${a:$fri_str}| $y${c:$sd_str}| stopped | N/A | $z "
fi
RETVAL=$?
done
else
ps aux | grep -w $i | grep -v grep | grep -vw "bash -c"|grep -vw "sh" > /dev/null
ret=$?
if [ $ret -eq 0 ]; then
echo "$i${a:$fri_str}| $y${c:$sd_str}| stopped | N/A | N/A "
else
echo "$i${a:$fri_str}| $y${c:$sd_str}| stopped | N/A | N/A "
fi
RETVAL=$?
fi
done
done
echo
}

start_app()
{
if [ -e /home/oracle/bea/start$instance.sh ];then
ps aux | grep -w $instance |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$instance[^-]" >/dev/null
ret=$?
if [ $ret -eq 0 ];then
echo "$instance is Alive"
exit
else
cd /home/oracle/bea
sh start"$instance".sh >/dev/null
fi
else
if [ $instance = AdminServer ];then
echo "$instance is administrator, please hand start"
exit
else
echo "Usage: $0 $1 {[intance_name]}"
fi
fi
}

stop_app()
{
if [ -e /home/oracle/bea/stop$instance.sh ];then
str=`echo $instance | awk '{print length($1)}'`
ps aux | grep -w $instance |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$instance[^-]" >/dev/null
ret=$?
if [ $ret -ne 0 ];then
echo "$instance is Down"
else
cd /home/oracle/bea
sh stop$instance.sh >/dev/null
sleep 1
ps aux | grep -w $instance |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$instance[^-]" >/dev/null
ret=$?
if [ $ret -ne 0 ];then
echo -e "Stopping $instance from Script:${c:$str} \033[32m [OK] \033[0m"
else
echo -e "Stopping $instance from Script:${c:$str} \033[31m [false] \033[0m"
kill -9 `ps aux | grep -w $instance |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$instance[^-]"|awk '{print $2}'` 2>/dev/null
sleep 2
ps aux | grep -w $instance |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$instance[^-]" >/dev/null
ret=$?
if [ $ret -ne 0 ];then
echo -e "Stopping $instance from Kill: ${c:$str} \033[32m [OK] \033[0m"
else
echo -e "Stopping $instance from Kill: ${c:$str} \033[31m [false] \033[0m"
fi
fi
fi
else
if [ $instance = AdminServer ];then
echo "$instance is administrator, please hand stop"
else
echo "Usage: $0 $1 {[intance_name]}"
fi
fi
}

startall_app()
{
for i in `ls -l $WEBLOGIC_HOME| awk '/^d/{ print $NF}'`
do
for y in `ls -l $WEBLOGIC_HOME/$i/servers 2>&1|awk '/^d/{ print $NF}'`
do
str=`echo $y | awk '{print length($1)}'`
ps aux | grep -w $y | grep -v "grep"| grep -w "Dweblogic.Name"|grep "$y[^-]" > /dev/null
ret=$?
if [ $ret -eq 0 ]; then
echo "$y is Alive"
else
if [ $y = AdminServer ];then
echo "$y is administrator, please hand start"
elif [ $y = domain_bak ];then
echo "$y is't need running"
else
cd /home/oracle/bea
sh start"$y".sh 2>/dev/null
sleep 10
ps aux | grep -w $y | grep -v "grep"| grep -w "Dweblogic.Name"|grep "$y[^-]" > /dev/null
ret=$?
if [ $ret -eq 0 ]; then
echo -e "Starting $y :${c:$str} \033[32m [OK] \033[0m"
else
echo -e "Starting $y :${c:$str} \033[31m [false] \033[0m"
fi
fi
fi
done
done
}

stopall_app()
{
for i in `ls -l $WEBLOGIC_HOME| awk '/^d/{ print $NF}'`
do
for y in `ls -l $WEBLOGIC_HOME/$i/servers 2>&1|awk '/^d/{ print $NF}'`
do
str=`echo $y | awk '{print length($1)}'`
ps aux | grep -w $y |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$y[^-]" >/dev/null
ret=$?
if [ $ret -ne 0 ];then
echo "$y is Down"
else
if [ $y = AdminServer ];then
echo "$y is administrator, please hand stop"
else
cd /home/oracle/bea
sh stop"$y".sh 2>/dev/null
sleep 1
ps aux | grep -w $y |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$y[^-]" >/dev/null
ret=$?
if [ $ret -ne 0 ];then
echo -e "Stopping $y from Script:${c:$str} \033[32m [OK] \033[0m"
else
echo -e "Stopping $y from Script:${c:$str} \033[31m [false] \033[0m"
sleep 2
kill -9 `ps aux | grep -w $y |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$y[^-]"|awk '{print $2}'` 2>/dev/null
sleep 1
ps aux | grep -w $y |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$y[^-]" >/dev/null
ret=$?
if [ $ret -ne 0 ];then
echo -e "Stopping $y from Script:${c:$str} \033[32m [OK] \033[0m"
else
echo -e "Stopping $y from Script:${c:$str} \033[31m [false] \033[0m"
fi

fi
fi
fi
done
done
}

info_log()
{
str=`echo $instance | awk '{print length($1)}'`
x=0
while((x<180))
do
x=$(($x+1))
now_num=`wc -l /opt/log/$instance.log 2>/dev/null | awk '{print $1}'`
sleep 1
new_num=`wc -l /opt/log/$instance.log 2>/dev/null | awk '{print $1}'`
result=$(($new_num-$now_num))
file_info=`tail -n $result /opt/log/$instance.log | awk '{if(($10~/<Server/) && ($13~/RUNNING/)) print}'`
number=`echo $file_info | wc -c | awk '{print $1}'`
if [ $number -gt 5 ];then
tail -n $result /opt/log/$instance.log
echo
ps aux | grep -w $instance |grep -v "grep"| grep -w "Dweblogic.Name"|grep "$instance[^-]" >/dev/null
ret=$?
if [ $ret -eq 0 ];then
echo -e "Starting $instance :${c:$str} \033[32m [OK] \033[0m"
else
echo -e "Starting $instance :${c:$str} \033[31m [false] \033[0m"
fi
exit
else
tail -n $result /opt/log/$instance.log
fi
done
}
print_help()
{
echo
echo "Permitted <scope>/<command>/<options> combinations are:"
echo
echo " scope command instance"
echo "------- --------- ----------"
echo "[<scope>] status - Get managed instance status"
echo "[<scope>] status -p - Get managed instance status and ports"
echo "[<scope>] status -app - Get managed instance status and applications"
echo "[<scope>] start [<instance_name>] - Start weblogic instance"
echo "[<scope>] stop [<instance_name>] - Stop weblogic instance"
echo "[<scope>] restart [<instance_name>] - restart weblogic instance"
echo "[<scope>] startall - Start all Down weblogic instance"
echo "[<scope>] stopall - Stop all Alive weblogic instance"
echo "[<scope>] restartall - Restart all weblogic instance"
echo
}
if [ $# -eq 0 ];then
echo
echo "The Script Version : 1.0"
echo "The Script Update time : 2011-12-15 10:54"
echo "Usage: $0 -help"
fi

if [ $# -eq 1 ];then
if [ $1 = status ];then
sprint_status
elif [ $1 = -help ];then
print_help
elif [ $1 = startall ];then
startall_app
elif [ $1 = stopall ];then
stopall_app
elif [ $1 = restartall ];then
stopall_app
sleep 5
startall_app
else
echo "Usage: $0 {-help}"
fi
fi

if [ $# -eq 2 ];
then
case "$1" in
status)
if [ $2 = -help ];then
print_help
elif [ $2 = -app ];then
sprint_status_app
elif [ $2 = -p ];then
sprint_status_port
else
echo "Usage: $0 $1 {-help|-app}"
fi
;;
start)
start_app
info_log
;;
stop)
stop_app
;;
restart)
stop_app
sleep 2
start_app
info_log
;;
*)
esac
fi

posted @ 2020-05-06 13:10  流年浮生  阅读(440)  评论(0)    收藏  举报