新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
由于需要对多个项目的多个应用进行管理,项目路径已经做了标准化部署,所以需要一个统一的脚本来管理所有的启停操作。
创新互联长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为浉河企业提供专业的网站设计、做网站,浉河网站改版等技术服务。拥有10年丰富建站经验和众多成功案例,为您定制开发。里面附带日志、进程启动检查、批量状态查看等功能。
以下为脚本相关内容,有些写法有点low,暂时没有想到更好的写法,有兴趣的可以留言
#!/bin/bash #用途:本脚本用来管理tomcat节点的启动、停止、重启,以及查看tomcat节点的状态 #作者:张晓龙 #版本v1.0 #定义帮助说明函数,语法错误时返回使用说明 template(){ echo "Usage: $0 -n appname -i node action" echo "-n appname:specifies the application name" echo "-i node:specifie node number of application,it must followed by the -n option." echo "If the node is all,all nodes of application will be operated. " echo "action start/restart/stop:start or stop or restart the specified application node" exit 101 } #使用getopt获取应用名称、节点数,节点数可以为所有 RST=$(getopt -o n:i: -- "$@") if [ $? -ne 0 ];then template fi eval set -- "$RST" while [ -n "$1" ] do case "$1" in -n) if [[ "$2" =~ -. ]];then #语法错误 template; fi if [[ "$3" =~ -. ]];then APPNAME="$2"; fi if [[ $(ps aux | grep java|grep ${APPNAME}|wc -l) == 0 ]];then template; shift 2 fi;; -i) if [[ "$2" =~ -. ]];then#语法错误 template fi if [[ "$3" =~ -. ]];then NODE="$2"; #判断输入的节点,如果不是数字或者all,则进行报错并退出 if [[ $(echo ${NODE}|grep -E '^[0-9]+$|all') == "" ]];then echo "Error type!Argment with $1 must be init or all!" template fi shift 2 fi;; --) shift; break;; *) template break;; esac done #全局初始化变量定义 init_args(){ export DAY=$(date +%Y-%m-%d) export TIME=$(date +%H-%M-%S) export ALIVE=$(echo -e "\e[1;32m[ALIVE]\e[0m") export NULL=$(echo -e "\e[1;31m[N/A]\e[0m") export ERROR=$(echo -e "\e[1;31m[ERROR]\e[0m") export WARNING=$(echo -e "\e[1;33m[WARNING]\e[0m") [ ! -d ${HOME}/scripts/logs ];mkdir -p ${HOME}/scripts/logs [ ! -d ${HOME}/scripts/logs/tomcat.log ];touch ${HOME}/scripts/logs/tomcat.log export LOG_FILE=${HOME}/scripts/logs/tomcat.log } #日志模块 log(){ DATE=$(date +"[%Y-%m-%d %H:%M:%S]") echo ${DATE} $* echo ${DATE} $* >> ${LOG_FILE} } #tomcat启动定时器,低于60秒继续执行脚本,高于记录报错信息 timeout_start() { WAITFOR=60 COMMAND=$* ${COMMAND} & COMMANDPID=$! ( sleep ${WAITFOR} ; echo '1' >temp.log && kill -9 ${COMMANDPID} >/dev/null 2>&1 ) & SLEEPPID=${PPID} wait ${COMMANDPID} >/dev/denull 2>&1 kill ${SLEEPPID} > /dev/null 2>&1 } check_err(){ if [[ $(cat temp.log) == 1 ]];then rm temp.log log $* fi } start(){ WORK_DIR=${HOME}/${APPNAME} if [[ "${NODE}" == all ]];then #检查是否有进程存活,如果有,做出判断,是杀掉后再启动,还是保留,只启动未启动的 NODE_NUMS=$(ls ${WORK_DIR}|grep tomcat_${APPNAME}.*|wc -l) for i in $(seq 1 ${NODE_NUMS}) do NODE_PID=$(ps aux | grep java|grep tomcat_${APPNAME}_$i|grep -v grep|awk '{print $2}') if [[ $(echo ${#NODE_PID}) != 0 ]];then read -p "${WARNING} Process ${APPNAME} node $i is running now!Do you want to restart it?(Y|N)" input case $input in Y|y) log " stop ${APPNAME} node is $i pid is ${NODE_PID}}" kill -9 ${NODE_PID};; n|N) echo "${WARNING}: ${APPNAME} node $i pid ${NODE_PID} is already start!" continue;; esac else cd ${WORK_DIR}/tomcat_${APPNAME}_$i/bin log " start ${APPNAME} node is $i pid is ${NODE_PID}" sh startup.sh > /dev/null 2>&1 timeout_start tail -f ../logs/catalina.${DAY}.log | while read line do TAIL_PID=$(ps aux | grep "tail -f ../logs/catalina.${DAY}.log"|awk '{print $2}') if [[ $line =~ .*Server\ startup\ in.* ]];then kill -9 ${TAIL_PID} fi done check_err " ${ERROR} ${APPNAME} node $i start timeout!" fi done #如果指定了节点,那么就判断节点是否是启动状态,如果启动,杀掉后再启动 elif [[ ${NODE} != all ]];then NODE_PID=$(ps aux | grep tomcat_${APPNAME}_${NODE}|grep -v grep|awk '{print $2}') if [[ $(echo ${#NODE_PID}) != 0 ]];then read -p "${WARNING} Process ${APPNAME} node ${NODE_PID} is running now!Do you want to restart it?(Y|N)" input case $input in Y|y) log " stop ${APPNAME} node is ${NODE} pid is ${NODE_PID}}" kill -9 ${NODE_PID};; n|N) echo "${WARNING}: ${APPNAME} node ${NODE} pid ${NODE_PID} is already start!" continue;; esac else cd ${WORK_DIR}/tomcat_${APPNAME}_${NODE}/bin sh startup.sh > /dev/null 2>&1 timout_start tail -f ../logs/catalina.${DAY}.log| while read line do TAIL_PID=$(ps aux | grep "tail -f ../logs/catalina.${DAY}.log"|awk '{print $2}') if [[ $line =~ .*Server\ startup\ in.* ]];then kill -9 ${TAIL_PID} fi done check_err " ${ERROR} ${APPNAME} node ${NODE} start timeout!" fi fi } stop(){ if [[ "${NODE}" == all ]];then NODE_PID=$(ps aux | grep java | grep tomcat_${APPNAME}| grep -v grep | awk '{print $2}') m=0 for i in ${NODE_PID[@]} do m=$(expr $m+1) log " stop ${APPNAME} node $m pid is $i " kill -9 ${NODE_PID} done else NODE_PID=$(ps aux | grep java | grep tomcat_${APPNAME}_${NODE}|grep -v grep | awk '{print $2}') log " stop ${APPNAME} node ${NODE} pid is ${NODE_PID} " kill -9 ${NODE_PID} fi } status(){ echo "System time is : ${DAY} ${TIME}" echo "${APPNAME} proccess status, node is ${NODE}" echo " ------------------------------------------------- " printf "|\t%-10s|\t%-10s|\t%-10s|\n" namepidstatus echo " ------------------------------------------------- " if [[ "${NODE}" == all ]];then NODES=$(ls ${HOME}/${APPNAME}|grep tomcat_${APPNAME}| wc -l) for i in $(seq 1 ${NODES}) do cd ${HOME}/${APPNAME} NODE_NAME=$(ls tomcat_${APPNAME}_${i}) NODE_PID=$(ps aux | grep java | grep ${NODE_NAME}|grep -v grep | awk '{print $2}') if [[ $(echo ${#NODE_PID}) == 0 ]];then NODE_STATUS=${NULL} else NODE_STATUS=${ALIVE} fi printf "|\t%-10s|\t%-10s|\t%-10s|\n" ${NODE_NAME} ${NODE_PID} ${NODE_STATUS} done else NODE_NAME=$(ls ${HOME}/${APPNAME}/tomcat_${APPNAME}_${NODE}) NODE_PID=$(ps aux | grep java | grep ${NODE_NAME}|grep -v grep | awk '{print $2}') if [[ $(echo ${#NODE_PID}) == 0 ]];then NODE_STATUS='N/A' else NODE_STATUS='Alive' fi printf "|\t%-10s|\t%-10s|\t%-10s|\n"${NODE_NAME} ${NODE_PID} ${NODE_STATUS} fi echo " ------------------------------------------------- " } #变量引用 init_args #执行动作 case $1 in start) start;; stop) stop;; restart) stop; start;; status) status;; *) template;; esac
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。