4-11 2 views
工作中经常会用到写一些服务启动脚本,当然python的我一般还是会用supervisor去管理,但是公司还是Java、NodeJS、GO语言开发的较多一些,下面这个脚本是对Java SpringBoot开发框架开发的管理后台和API的启动脚本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
#!/bin/env bash # Author: Eric Winn # Email: eng.eric.winn@gmail.com # # SpringBoot startup script for the SpringBoot server # # chkconfig: 345 80 20 # description: start the SpringBoot deamon # doer=`whoami` if [ "${doer}" != 'root' ];then echo "Services has to be done by root user" exit 1 fi # Source function library #. /etc/rc.d/init.d/functions # Path to the SpringBoot binary. RETVAL=0 test -r /etc/profile.d/app.sh && . /etc/profile.d/app.sh userName="tomcat" HOME="/var/app" screenExec="/usr/bin/screen" modeEnv=product JavaExec=${JAVA_HOME}/bin/java envMode=dev start() { modeName=${1} app_port=${2} appName="${HOME}/webapps/${modeName}/${modeName}-1.0.jar" [ -x $JavaExec ] || exit 5 [ -x $screenExec ] || exit 5 _pid=`ps -ef |grep java |grep ${modeName} |grep -v grep |grep -v SCREEN |awk '{print $2}'` if [ -n "$_pid" ]; then echo "${modeName} [${app_port}] has already running!!!" return else echo -n $"Starting ${modeName} [${app_port}]: " screenStart="cd ${HOME} ; ${screenExec} -L -t ${modeName} -dmS ${modeName} ${JavaExec} -jar ${appName} --spring.profiles.active=${envMode}" su - ${userName} --shell="/bin/bash" -c "${screenStart}" fi retval=$? [ $retval -eq 0 ] && echo -e "\t\tsuccessfull..." return $retval } stop() { modeName=${1} app_port=${2} _pid=`ps -ef |grep java | grep ${modeName} |grep -v grep |grep -v SCREEN |awk '{print $2}'` test -z "$_pid" && echo "${modeName} [${app_port}] has already stopped!!!" && return echo -n $"Stopping ${modeName} [${app_port}]: " su - ${userName} --shell="/bin/bash" -c "${screenExec} -S ${modeName} -X quit" >/tmp/${modeName}.log 2>&1 retval=$? [ $retval -eq 0 ] && echo -e "\t\tsuccessfull..." && return $retval [ $retval -ne 0 ] && ps -ef |grep ${JavaExec} |grep -v grep |awk -F' ' '{print $2}' | xargs kill -9 >>/tmp/${modeName}.log 2>&1 retval=$? return $retval } # See how we were called. case "$1" in start|stop) ${1} "${2}" "${3}" ;; restart) stop "${2}" "${3}" sleep 3 start "${2}" "${3}" ;; *) echo $"Usage: $prog {start|stop|restart} {admin 8085|api 8080} {|}" exit 1 ;; esac |
如果想赏钱,可以用微信扫描下面的二维码,一来能刺激我写博客的欲望,二来好维护云主机的费用; 另外再次标注博客原地址 itnotebooks.com 感谢!