启动:
#!/bin/bashset -eJAVA_HOME=/usr/local/java # 检查是否有项目名appName=$1if [ "$appName" == "" ];then echo "appName is needed!" 2>&2 exit 1fi # 输出脚本所在的真正目录,存储到变量中bin_abs_path=$(readlink -f $(dirname $0))base=${bin_abs_path}/..echo "start application $appName ..." 2>&2 # 检查pid文件是否存在if [ -f $bin_abs_path/$appName.pid ] ; then echo "found $appName.pid , Please run stop-ndb.sh first ,then startup-ndb.sh" 2>&2 exit 1fi # 启动项目JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:-UseGCOverheadLimit -XX:MaxPermSize=128m -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8 -Duser.language=zh -Duser.region=CN -Duser.timezone=Asia/Shanghai"APP_OPTS="-DappName=$appName -Dspring.profiles.active=envProd"nohup $JAVA_HOME/bin/java $JAVA_OPTS $APP_OPTS -jar $base/app/$appName.jar 1>/dev/null 2>/dev/null &sleep 20 # 记录Shell最后运行的后台Process的PIDecho $! > $bin_abs_path/$appName.pidecho "start application $appName success!" 2>&2
停止:
#!/bin/bashset -e # 检查是否有项目名appName=$1if [ "$appName" == "" ];then echo "appName is needed!" 2>&2 exit 1fi # 输出脚本所在的真正目录,存储到变量中bin_abs_path=$(readlink -f $(dirname $0))base=${bin_abs_path}/.. # 检查jar包是否存在if [ ! -e "$base/app/$appName.jar" ];then echo "$base/$appName.jar is not found!" 2>&2 exit 1fi # 得到对应的进程id的函数getapp_pid() { JAVA_PID=`ps -C java -f --width 1000|grep "$appName"|grep -v grep|awk '{print $2}'` echo $JAVA_PID;}echo "stop application $appName ..." 2>&2# 确定pid pidfile=$base/bin/$appName.pidpid="";if [ -f $pidfile ];then pid=`cat $pidfile`fi #若有没有相应的pid文件,将通过查询程序名称的进程获得;if [ "$pid" == "" ];then pid=`getapp_pid "appName=$appName"`fiif [ "$pid" != "" ];then echo -e "`hostname`: stopping $appName $pid ... " 2>&2 gpid=`ps -C java -f --width 1000|grep "$pid"|grep -v grep|awk '{print $2}'` if [ "$gpid" == "$pid" ] ; then kill $pid fifiLOOPS=0while (true);doecho "stoping..." gpid=`ps -C java -f --width 1000|grep "$appName"|grep -v grep|awk '{print $2}'` if [ "$gpid" == "" ];then echo "Oook! cost:$LOOPS" 2>&2 if [ -f $pidfile ];then `rm $pidfile` fi break; fi let LOOPS=LOOPS+1 sleep 1done