由于项目的原因,需要帮产品对很多tomcat应用频繁的重启。一段时间后我决定写一个Shell脚本来简化这个繁琐的工作,并带有交互界面可以让策划和产品人员使用。脚本完成后已使用了几个月,比较稳定。回想当时入门Shell时,参考了不少资料,没有发现一个大小适中的,适合自己学习的实用资料。这里贴出脚本,主要是方便新手学习。本例中用到不少我知道的技巧,大小也适中,比较适合入门。但毕竟平时写shell不多,水平有限,熟手若发现什么问题,欢迎指正、讨论。
#!/bin/bash
#server manager
# version 1.0
# Author:jaden
#====Read Me=====
# This is mainly designed for the non-programmers to manager servers
# with basic operation. More function would be added in the future.
# It also could provide convinience for the developers.
# Wish you enjoy it.
#===Rely On=====
# tongbu rely on tongbu.sh in every tomcat bin directory
#===technique====
# Process shutdown and startup once executed, they will not be sighuped
# by closing the terminal. However, tailf commond will sighuped.
#Settings start
path="/home/app"
#Settings end
#viarables
processarr=""
selectserver=""
function shutdown {
# here need the server name
servername=$1
cd "$path/$servername"
# check if the server is running
pid=`processpid $servername`
if [ -z "$pid" ]
then
echo -e "\t$servername is already down"
return
else
echo -e "\t$servername is running"
fi
# start OR restart a server
echo -e "\t$servername is shuting down"
nohup sh bin/shutdown.sh > /dev/null 2>&1 &
echo -en "\tPlease wait in 20 seconds"
for (( i=1 ; i<=20 ; i++))
do
sleep 1s
echo -n "."
pid=`processpid $servername`
if [ -z "$pid" ]
then
echo -e "\n\t$servername is already down"
break
fi
done
echo
pid=`processpid $servername`
if [ -z "$pid" ]
then
echo -e "\t\e[1;32m[shutdown] $servername shutdown successfully\e[0m"
else
echo -e "\t$servername is going to be killed"
killpid $pid
echo -e "\t\e[1;32m[shutdown] $servername was killed\e[0m"
fi
}
function startup {
# here need the server name
servername=$1
cd "$path/$servername"
# shutdown firstly
shutdown $servername
# then start
echo -e "\t\e[1;32m[startup] $servername is starting..., \
it will take few minutes\e[0m"
nohup sh bin/startup.sh > /dev/null 2>&1 &
}
function startwatch {
# here need the server name
startup $1
sleep 2s
watch $servername
}
function watch {
# here need the server name
servername=$1
cd "$path/$servername"
tailf logs/catalina.out
}
#rely on tongbu.sh
function tongbu {
servername=$1
cd "$path/$servername"
#shutdown firstly
shutdown $servername
#tongbu secondly
result=`sh bin/tongbu.sh | grep 'rsync success'`
if [ -n "$result" ]
then
echo -e "\t\e[1;32m[tongbu] tongbu success!\e[0m"
sleep 1s
#start&watch thirdly
startwatch $servername
else
echo
echo -e "\ttongbu failed!"
sleep 2s
fi
}
function processpid {
# here need the server name
ps -ef | sed -n "/$1\s/p" | grep $path | gawk '{print $2}'
}
#processarr
function serverarr {
cd $path
processarr=($(ls -l | grep tomcat | sed -n '/\.gz$/!p' | \
sed -n '/\.bz2$/!p' | gawk '{print $9}'))
}
function printprocesslist {
echo -e "\tThere is/are ${#processarr[@]} server(s)"
printf "\t%-8s %-25s %-10s %-5s\n" No. Server Status Pid
num=1
for process in ${processarr[*]}
do
pid=`processpid $process`
if [ -n "$pid" ] #how to take the contrary
then
status="running"
else
status="idle"
fi
printf "\t%-8s %-25s %-10s %-5s\n" $num $process $status $pid
num=$[ $num + 1 ]
done
}
#Only positive number could pass
function checkfunction {
if [ -z $1 ]
then
echo
echo -e "\tLong time on operation. Exit.."
exit
fi
# if it's not a number
if ! expr $1 + 10 &>/dev/null
then
echo -e "\tWrong type in. Exit.."
exit
fi
if [ $1 -lt 0 ]
then
echo -e "\tWrong type in. Exit.."
exit
fi
if [ $1 -eq 0 ]
then
echo -e "\tExit.."
exit
fi
}
#selectprocess
function selectprocess {
echo
echo -e "\tPlease only type Number (0 to quit)"
echo -en "\tSelect a server:"
read -t 30 command
checkfunction $command
if [ $command -gt ${#processarr[@]} ]
then
echo -e "\tWrong selection!"
exit
fi
selectserver="${processarr[ $[$command-1] ]}"
echo
echo -en "\tthe select server is \e[1;31m$path/$selectserver\t\e[0m"
echo -en " \e[1;32mbe carefull\e[0m"
for ((i=1;i<3;i++))
do
sleep 1s
echo -en "\e[1;32m.\e[0m"
done
echo
}
function killpid {
kill -9 $1
}
function menu {
command=""
selectserver=""
clear
echo -e "\t\tWelcome to the Server Manager System"
echo
echo -e "\tYou can press Ctrl + C to Quit the system at any time"
echo -e "\tIf you have any question, please consult a server programmer"
echo
echo -e "\tSystem Time:\t`date`"
echo -e "\tWorking path:\t$path"
echo
serverarr
printprocesslist
selectprocess
echo
echo -e "\t0. quit\t\t\tto quit the manager system"
echo -e "\t1. startup\t\tto start OR restart a server"
echo -e "\t2. shutdown\t\tto shutdown a server"
echo -e "\t3. start&watch\t\tto start OR restart a server and \
then watch the process"
echo -e "\t4. reselect\t\tto reselect a server"
echo -e "\t5. watch\t\tto watch the processing"
echo -e "\t9. tongbu\t\tto shutdown, tongbu and then start a server \
\e[1;31m(Only for developers!)\e[0m"
echo
echo -en "\tPlease type in the command:"
command=""
read -t 30 command
checkfunction $command
}
function show {
while [ 1 ]
do
menu
case $command in
1)
#startup
startup $selectserver
sleep 6s
;;
2)
#shutdown
shutdown $selectserver
sleep 2s
;;
3)
#startwatch
startwatch $selectserver
;;
4)
#reselect
;;
5)
watch $selectserver
;;
9)
tongbu $selectserver
;;
0)
echo
exit
;;
*)
echo -e "\tPlease type the right commond"
sleep 2s
;;
esac
done
}
show