运行Jar包程序Shell

 

启动:

#!/bin/bash
set -e
JAVA_HOME=/usr/local/java
# 检查是否有项目名 appName
=$1 if [ "$appName" == "" ];then echo "appName is needed!" 2>&2 exit 1 fi
# 输出脚本所在的真正目录,存储到变量中 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 1 fi

# 启动项目 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的PID echo $! > $bin_abs_path/$appName.pid echo "start application $appName success!" 2>&2

 

停止:

#!/bin/bash
set -e
# 检查是否有项目名 appName
=$1 if [ "$appName" == "" ];then echo "appName is needed!" 2>&2 exit 1 fi
# 输出脚本所在的真正目录,存储到变量中 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 1 fi
# 得到对应的进程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.pid pid=""; if [ -f $pidfile ];then pid=`cat $pidfile` fi
#若有没有相应的pid文件,将通过查询程序名称的进程获得;
if [ "$pid" == "" ];then pid=`getapp_pid "appName=$appName"` fi if [ "$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 fi fi LOOPS=0 while (true); do echo "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 1 done

 

posted @ 2017-10-11 19:53  简笔话_Golden  阅读(1137)  评论(0编辑  收藏  举报