跳至主要內容

服务启动脚本

soulballad架构生产配置生产配置约 742 字大约 2 分钟

6.服务启动脚本

service-op.sh

#!/bin/bash
## -------------------------------------------------------------------------
export PATH=/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin
export TERM="xterm-256color"
export WORKDIR="$( cd $(dirname "$0") &&  pwd )"
cd "${WORKDIR}" || exit 1
## -------------------------------------------------------------------------
info() {
    tput bold ; tput setaf 2; date +"%F %T Info: $@" ; tput sgr0
}

warn() {
    tput bold ; tput setaf 3; date +"%F %T Warning: $@" ; tput sgr0
}

error() {
    tput bold ; tput setaf 1; date +"%F %T Error: $@" ; tput sgr0
}

err_exit() {
    tput bold ; tput setaf 1; date +"%F %T Error: $@" ; tput sgr0
    exit 1
}
## -------------------------------------------------------------------------
help_msg() {
    echo -e "Usage:"
    echo -e "    bash  $( basename $0 )  build     Deploy the tomcat-file service."
    echo -e "    bash  $( basename $0 )  start     Start the tomcat-file service."
    echo -e "    bash  $( basename $0 )  stop      Stop the tomcat-file service."
    echo -e "    bash  $( basename $0 )  check     Check the tomcat-file service."
    echo -e "    bash  $( basename $0 )  restart   Retart the tomcat-file service."
}
## -------------------------------------------------------------------------
check_env() {
    CONFIG="${WORKDIR}/env.conf"
    if [[ ! -f ${CONFIG} ]] ; then
	    err_exit "$( basename ${CONFIG} ) is not found!"
    else
	    . "${CONFIG}"
    fi
    var_arrs=(TOMCAT_FILE_JAR_PACKAGE_NAME TOMCAT_FILE_JAVA_OPTS)
    for var in ${var_arrs[@]}
    do
        var2=`eval echo '$'"$var"`
	if [[ -z "${var2}" ]] ; then
            err_exit "${var} is empty!"
	fi
    done
}
## -------------------------------------------------------------------------
check_jar(){
    check_env
    if ! [[ -f "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} ]]; then
        err_exit "Not found the tomcat-file jar package!"
    fi
}
## -------------------------------------------------------------------------
build (){
    check_env
    check_jar
    if ! ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
          nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test  >> "${WORKDIR}"/catalina.out 2>&1 &
          info "Waiting for 10 seconds."
          sleep 10
          if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
              info "tomcat-file build success."
	  else
              err_exit "tomcat-file build failed."
          fi
    else
	  info "tomcat-file has already build."
    fi
}
## -------------------------------------------------------------------------
start (){
    check_env
    check_jar
    if ! ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
        nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 &
        info "Waiting for 10 seconds."
        sleep 10
        if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
            info "tomcat-file starting success."
        else
            err_exit "tomcat-file starting failed."
        fi
    else
        info "tomcat-file has already running."
    fi
}
## -------------------------------------------------------------------------
stop (){
    check_env
    check_jar
    if ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
	kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'`
	if [[ $? -eq 0 ]]; then
	    info "tomcat-file stop success."
	else
	    err_exit "tomcat-file stop failed."
	fi
    else
	info "tomcat-file has already stop."
    fi
}
## -------------------------------------------------------------------------
restart(){
    check_env
    check_jar
    if  ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
	kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'`
        nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 &
        info "Waiting for 10 seconds."
        sleep 10
	if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
	   info "tomcat-file restart success."
	else
	   err_exit "tomcat-file restart failed."
	fi
    else
        info "Waiting for 10 seconds."
        sleep 10
        nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 &
	if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
	   info "tomcat-file restart success."
	else
	   err_exit "tomcat-file restart failed."
	fi	
    fi
}
## -------------------------------------------------------------------------
clear (){
    info "开始卸载tomcat-file服务."
    read -r -p "Are You Sure? [Y/n] " input
    case $input in
        [yY][eE][sS]|[yY])
            check_env
            if ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
               kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'` && rm -rf "${WORKDIR}"/catalina*
               if [[ $? -eq 0 ]]; then
       	          info "tomcat-file clear success."
               else
                  err_exit "tomcat-file clear failed."
               fi
            else
               info "tomcat-file has already clear."
            fi
        ;;
        [nN][oO]|[nN])
            exit 1
        ;;

        *)
            echo "Invalid input..."
            exit 1
        ;;
        esac
}
## -------------------------------------------------------------------------
check (){
    check_env
    check_jar
    if ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
       info "tomcat-file is running."
    else
       err_exit "tomcat-file is not running."
    fi
}
## -------------------------------------------------------------------------
case $1 in
    build )
        build
    ;;
    start )
	start
    ;;
    stop )
	stop
    ;;
    restart )
	restart
    ;;
    check )
	check
    ;;
    clear )
	clear
    ;;
    * )
        help_msg
    ;;
esac

env.conf

# tomcat-file服务jar包名称,例如: 
export TOMCAT_FILE_JAR_PACKAGE_NAME="vcsp-file-0.0.1-SNAPSHOT.jar"

# tomcat-file服务java启动参数
# 例如:-Xms4096m -Xmx4096m -Xmn256m -Xss256k -XX:PermSize=64m -XX:MaxPermSize=128m,根据实际情况填写
export TOMCAT_FILE_JAVA_OPTS="-Xms4096m -Xmx4096m -Xmn256m -Xss256k -XX:PermSize=64m -XX:MaxPermSize=128m"

上次编辑于:
贡献者: soulballad
"$var"` if [[ -z "${var2}" ]] ; then err_exit "${var} is empty!" fi done } ## ------------------------------------------------------------------------- check_jar(){ check_env if ! [[ -f "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} ]]; then err_exit "Not found the tomcat-file jar package!" fi } ## ------------------------------------------------------------------------- build (){ check_env check_jar if ! ps -ef | grep -v grep | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 & info "Waiting for 10 seconds." sleep 10 if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then info "tomcat-file build success." else err_exit "tomcat-file build failed." fi else info "tomcat-file has already build." fi } ## ------------------------------------------------------------------------- start (){ check_env check_jar if ! ps -ef | grep -v grep | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 & info "Waiting for 10 seconds." sleep 10 if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then info "tomcat-file starting success." else err_exit "tomcat-file starting failed." fi else info "tomcat-file has already running." fi } ## ------------------------------------------------------------------------- stop (){ check_env check_jar if ps -ef | grep -v grep | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'` if [[ $? -eq 0 ]]; then info "tomcat-file stop success." else err_exit "tomcat-file stop failed." fi else info "tomcat-file has already stop." fi } ## ------------------------------------------------------------------------- restart(){ check_env check_jar if ps -ef | grep -v grep | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'` nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 & info "Waiting for 10 seconds." sleep 10 if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then info "tomcat-file restart success." else err_exit "tomcat-file restart failed." fi else info "Waiting for 10 seconds." sleep 10 nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 & if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then info "tomcat-file restart success." else err_exit "tomcat-file restart failed." fi fi } ## ------------------------------------------------------------------------- clear (){ info "开始卸载tomcat-file服务." read -r -p "Are You Sure? [Y/n] " input case $input in [yY][eE][sS]|[yY]) check_env if ps -ef | grep -v grep | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'` && rm -rf "${WORKDIR}"/catalina* if [[ $? -eq 0 ]]; then info "tomcat-file clear success." else err_exit "tomcat-file clear failed." fi else info "tomcat-file has already clear." fi ;; [nN][oO]|[nN]) exit 1 ;; *) echo "Invalid input..." exit 1 ;; esac } ## ------------------------------------------------------------------------- check (){ check_env check_jar if ps -ef | grep -v grep | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then info "tomcat-file is running." else err_exit "tomcat-file is not running." fi } ## ------------------------------------------------------------------------- case $1 in build ) build ;; start ) start ;; stop ) stop ;; restart ) restart ;; check ) check ;; clear ) clear ;; * ) help_msg ;; esac">服务启动脚本 | Soulballad博客
跳至主要內容

服务启动脚本

soulballad架构生产配置生产配置约 742 字大约 2 分钟

6.服务启动脚本

service-op.sh

#!/bin/bash
## -------------------------------------------------------------------------
export PATH=/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin
export TERM="xterm-256color"
export WORKDIR="$( cd $(dirname "$0") &&  pwd )"
cd "${WORKDIR}" || exit 1
## -------------------------------------------------------------------------
info() {
    tput bold ; tput setaf 2; date +"%F %T Info: $@" ; tput sgr0
}

warn() {
    tput bold ; tput setaf 3; date +"%F %T Warning: $@" ; tput sgr0
}

error() {
    tput bold ; tput setaf 1; date +"%F %T Error: $@" ; tput sgr0
}

err_exit() {
    tput bold ; tput setaf 1; date +"%F %T Error: $@" ; tput sgr0
    exit 1
}
## -------------------------------------------------------------------------
help_msg() {
    echo -e "Usage:"
    echo -e "    bash  $( basename $0 )  build     Deploy the tomcat-file service."
    echo -e "    bash  $( basename $0 )  start     Start the tomcat-file service."
    echo -e "    bash  $( basename $0 )  stop      Stop the tomcat-file service."
    echo -e "    bash  $( basename $0 )  check     Check the tomcat-file service."
    echo -e "    bash  $( basename $0 )  restart   Retart the tomcat-file service."
}
## -------------------------------------------------------------------------
check_env() {
    CONFIG="${WORKDIR}/env.conf"
    if [[ ! -f ${CONFIG} ]] ; then
	    err_exit "$( basename ${CONFIG} ) is not found!"
    else
	    . "${CONFIG}"
    fi
    var_arrs=(TOMCAT_FILE_JAR_PACKAGE_NAME TOMCAT_FILE_JAVA_OPTS)
    for var in ${var_arrs[@]}
    do
        var2=`eval echo '$'"$var"`
	if [[ -z "${var2}" ]] ; then
            err_exit "${var} is empty!"
	fi
    done
}
## -------------------------------------------------------------------------
check_jar(){
    check_env
    if ! [[ -f "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} ]]; then
        err_exit "Not found the tomcat-file jar package!"
    fi
}
## -------------------------------------------------------------------------
build (){
    check_env
    check_jar
    if ! ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
          nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test  >> "${WORKDIR}"/catalina.out 2>&1 &
          info "Waiting for 10 seconds."
          sleep 10
          if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
              info "tomcat-file build success."
	  else
              err_exit "tomcat-file build failed."
          fi
    else
	  info "tomcat-file has already build."
    fi
}
## -------------------------------------------------------------------------
start (){
    check_env
    check_jar
    if ! ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
        nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 &
        info "Waiting for 10 seconds."
        sleep 10
        if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
            info "tomcat-file starting success."
        else
            err_exit "tomcat-file starting failed."
        fi
    else
        info "tomcat-file has already running."
    fi
}
## -------------------------------------------------------------------------
stop (){
    check_env
    check_jar
    if ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
	kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'`
	if [[ $? -eq 0 ]]; then
	    info "tomcat-file stop success."
	else
	    err_exit "tomcat-file stop failed."
	fi
    else
	info "tomcat-file has already stop."
    fi
}
## -------------------------------------------------------------------------
restart(){
    check_env
    check_jar
    if  ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
	kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'`
        nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 &
        info "Waiting for 10 seconds."
        sleep 10
	if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
	   info "tomcat-file restart success."
	else
	   err_exit "tomcat-file restart failed."
	fi
    else
        info "Waiting for 10 seconds."
        sleep 10
        nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 &
	if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
	   info "tomcat-file restart success."
	else
	   err_exit "tomcat-file restart failed."
	fi	
    fi
}
## -------------------------------------------------------------------------
clear (){
    info "开始卸载tomcat-file服务."
    read -r -p "Are You Sure? [Y/n] " input
    case $input in
        [yY][eE][sS]|[yY])
            check_env
            if ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
               kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'` && rm -rf "${WORKDIR}"/catalina*
               if [[ $? -eq 0 ]]; then
       	          info "tomcat-file clear success."
               else
                  err_exit "tomcat-file clear failed."
               fi
            else
               info "tomcat-file has already clear."
            fi
        ;;
        [nN][oO]|[nN])
            exit 1
        ;;

        *)
            echo "Invalid input..."
            exit 1
        ;;
        esac
}
## -------------------------------------------------------------------------
check (){
    check_env
    check_jar
    if ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
       info "tomcat-file is running."
    else
       err_exit "tomcat-file is not running."
    fi
}
## -------------------------------------------------------------------------
case $1 in
    build )
        build
    ;;
    start )
	start
    ;;
    stop )
	stop
    ;;
    restart )
	restart
    ;;
    check )
	check
    ;;
    clear )
	clear
    ;;
    * )
        help_msg
    ;;
esac

env.conf

# tomcat-file服务jar包名称,例如: 
export TOMCAT_FILE_JAR_PACKAGE_NAME="vcsp-file-0.0.1-SNAPSHOT.jar"

# tomcat-file服务java启动参数
# 例如:-Xms4096m -Xmx4096m -Xmn256m -Xss256k -XX:PermSize=64m -XX:MaxPermSize=128m,根据实际情况填写
export TOMCAT_FILE_JAVA_OPTS="-Xms4096m -Xmx4096m -Xmn256m -Xss256k -XX:PermSize=64m -XX:MaxPermSize=128m"

上次编辑于:
贡献者: soulballad
"$var"` if [[ -z "${var2}" ]] ; then err_exit "${var} is empty!" fi done } ## ------------------------------------------------------------------------- check_jar(){ check_env if ! [[ -f "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} ]]; then err_exit "Not found the tomcat-file jar package!" fi } ## ------------------------------------------------------------------------- build (){ check_env check_jar if ! ps -ef | grep -v grep | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 & info "Waiting for 10 seconds." sleep 10 if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then info "tomcat-file build success." else err_exit "tomcat-file build failed." fi else info "tomcat-file has already build." fi } ## ------------------------------------------------------------------------- start (){ check_env check_jar if ! ps -ef | grep -v grep | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 & info "Waiting for 10 seconds." sleep 10 if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then info "tomcat-file starting success." else err_exit "tomcat-file starting failed." fi else info "tomcat-file has already running." fi } ## ------------------------------------------------------------------------- stop (){ check_env check_jar if ps -ef | grep -v grep | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'` if [[ $? -eq 0 ]]; then info "tomcat-file stop success." else err_exit "tomcat-file stop failed." fi else info "tomcat-file has already stop." fi } ## ------------------------------------------------------------------------- restart(){ check_env check_jar if ps -ef | grep -v grep | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'` nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 & info "Waiting for 10 seconds." sleep 10 if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then info "tomcat-file restart success." else err_exit "tomcat-file restart failed." fi else info "Waiting for 10 seconds." sleep 10 nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 & if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then info "tomcat-file restart success." else err_exit "tomcat-file restart failed." fi fi } ## ------------------------------------------------------------------------- clear (){ info "开始卸载tomcat-file服务." read -r -p "Are You Sure? [Y/n] " input case $input in [yY][eE][sS]|[yY]) check_env if ps -ef | grep -v grep | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'` && rm -rf "${WORKDIR}"/catalina* if [[ $? -eq 0 ]]; then info "tomcat-file clear success." else err_exit "tomcat-file clear failed." fi else info "tomcat-file has already clear." fi ;; [nN][oO]|[nN]) exit 1 ;; *) echo "Invalid input..." exit 1 ;; esac } ## ------------------------------------------------------------------------- check (){ check_env check_jar if ps -ef | grep -v grep | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then info "tomcat-file is running." else err_exit "tomcat-file is not running." fi } ## ------------------------------------------------------------------------- case $1 in build ) build ;; start ) start ;; stop ) stop ;; restart ) restart ;; check ) check ;; clear ) clear ;; * ) help_msg ;; esac">
跳至主要內容

服务启动脚本

soulballad架构生产配置生产配置约 742 字大约 2 分钟

6.服务启动脚本

service-op.sh

#!/bin/bash
## -------------------------------------------------------------------------
export PATH=/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin
export TERM="xterm-256color"
export WORKDIR="$( cd $(dirname "$0") &&  pwd )"
cd "${WORKDIR}" || exit 1
## -------------------------------------------------------------------------
info() {
    tput bold ; tput setaf 2; date +"%F %T Info: $@" ; tput sgr0
}

warn() {
    tput bold ; tput setaf 3; date +"%F %T Warning: $@" ; tput sgr0
}

error() {
    tput bold ; tput setaf 1; date +"%F %T Error: $@" ; tput sgr0
}

err_exit() {
    tput bold ; tput setaf 1; date +"%F %T Error: $@" ; tput sgr0
    exit 1
}
## -------------------------------------------------------------------------
help_msg() {
    echo -e "Usage:"
    echo -e "    bash  $( basename $0 )  build     Deploy the tomcat-file service."
    echo -e "    bash  $( basename $0 )  start     Start the tomcat-file service."
    echo -e "    bash  $( basename $0 )  stop      Stop the tomcat-file service."
    echo -e "    bash  $( basename $0 )  check     Check the tomcat-file service."
    echo -e "    bash  $( basename $0 )  restart   Retart the tomcat-file service."
}
## -------------------------------------------------------------------------
check_env() {
    CONFIG="${WORKDIR}/env.conf"
    if [[ ! -f ${CONFIG} ]] ; then
	    err_exit "$( basename ${CONFIG} ) is not found!"
    else
	    . "${CONFIG}"
    fi
    var_arrs=(TOMCAT_FILE_JAR_PACKAGE_NAME TOMCAT_FILE_JAVA_OPTS)
    for var in ${var_arrs[@]}
    do
        var2=`eval echo '$'"$var"`
	if [[ -z "${var2}" ]] ; then
            err_exit "${var} is empty!"
	fi
    done
}
## -------------------------------------------------------------------------
check_jar(){
    check_env
    if ! [[ -f "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} ]]; then
        err_exit "Not found the tomcat-file jar package!"
    fi
}
## -------------------------------------------------------------------------
build (){
    check_env
    check_jar
    if ! ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
          nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test  >> "${WORKDIR}"/catalina.out 2>&1 &
          info "Waiting for 10 seconds."
          sleep 10
          if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
              info "tomcat-file build success."
	  else
              err_exit "tomcat-file build failed."
          fi
    else
	  info "tomcat-file has already build."
    fi
}
## -------------------------------------------------------------------------
start (){
    check_env
    check_jar
    if ! ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
        nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 &
        info "Waiting for 10 seconds."
        sleep 10
        if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
            info "tomcat-file starting success."
        else
            err_exit "tomcat-file starting failed."
        fi
    else
        info "tomcat-file has already running."
    fi
}
## -------------------------------------------------------------------------
stop (){
    check_env
    check_jar
    if ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
	kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'`
	if [[ $? -eq 0 ]]; then
	    info "tomcat-file stop success."
	else
	    err_exit "tomcat-file stop failed."
	fi
    else
	info "tomcat-file has already stop."
    fi
}
## -------------------------------------------------------------------------
restart(){
    check_env
    check_jar
    if  ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
	kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'`
        nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 &
        info "Waiting for 10 seconds."
        sleep 10
	if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
	   info "tomcat-file restart success."
	else
	   err_exit "tomcat-file restart failed."
	fi
    else
        info "Waiting for 10 seconds."
        sleep 10
        nohup /usr/local/jdk1.8.0_291/bin/java ${TOMCAT_FILE_JAVA_OPTS} -jar "${WORKDIR}"/pkgs/${TOMCAT_FILE_JAR_PACKAGE_NAME} --spring.profiles.active=test >> "${WORKDIR}"/catalina.out 2>&1 &
	if ps -ef | grep java | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
	   info "tomcat-file restart success."
	else
	   err_exit "tomcat-file restart failed."
	fi	
    fi
}
## -------------------------------------------------------------------------
clear (){
    info "开始卸载tomcat-file服务."
    read -r -p "Are You Sure? [Y/n] " input
    case $input in
        [yY][eE][sS]|[yY])
            check_env
            if ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
               kill -9 `ps -ef | grep -v grep | grep "${TOMCAT_FILE_JAR_PACKAGE_NAME}" | awk '{print $2}'` && rm -rf "${WORKDIR}"/catalina*
               if [[ $? -eq 0 ]]; then
       	          info "tomcat-file clear success."
               else
                  err_exit "tomcat-file clear failed."
               fi
            else
               info "tomcat-file has already clear."
            fi
        ;;
        [nN][oO]|[nN])
            exit 1
        ;;

        *)
            echo "Invalid input..."
            exit 1
        ;;
        esac
}
## -------------------------------------------------------------------------
check (){
    check_env
    check_jar
    if ps -ef | grep -v grep | grep java | grep  "${TOMCAT_FILE_JAR_PACKAGE_NAME}" >/dev/null 2>&1; then
       info "tomcat-file is running."
    else
       err_exit "tomcat-file is not running."
    fi
}
## -------------------------------------------------------------------------
case $1 in
    build )
        build
    ;;
    start )
	start
    ;;
    stop )
	stop
    ;;
    restart )
	restart
    ;;
    check )
	check
    ;;
    clear )
	clear
    ;;
    * )
        help_msg
    ;;
esac

env.conf

# tomcat-file服务jar包名称,例如: 
export TOMCAT_FILE_JAR_PACKAGE_NAME="vcsp-file-0.0.1-SNAPSHOT.jar"

# tomcat-file服务java启动参数
# 例如:-Xms4096m -Xmx4096m -Xmn256m -Xss256k -XX:PermSize=64m -XX:MaxPermSize=128m,根据实际情况填写
export TOMCAT_FILE_JAVA_OPTS="-Xms4096m -Xmx4096m -Xmn256m -Xss256k -XX:PermSize=64m -XX:MaxPermSize=128m"

上次编辑于:
贡献者: soulballad