1 APP_NAME=my-application 2 3 #使用说明,用来提示输入参数 4 usage() { 5 echo "Usage: sh service.sh [start|stop|restart|status]" 6 exit 1 7 } 8 9 #检查程序是否在运行 10 is_exist(){ 11 pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'` 12 #如果不存在返回1,存在返回0 13 if [ -z "${pid}" ];then 14 return 1 15 else 16 return 0 17 fi 18 } 19 20 #启动方法 21 start(){ 22 is_exist 23 if [ $? -eq "0" ]; then 24 echo "${APP_NAME} is already running. pid=${pid} ." 25 else 26 # -XX:+PrintGCDateStamps -XX:-UseConcMarkSweepGC -XX:-UseParallelGC -XX:+PrintGCDetails -XX:+UseParallelGC 27 nohup java -jar -Dfie.encoding=UTF-8 -Xms1024m -Xms1024m -XX:MaxTenuringThreshold=10 -XX:-UseAdaptiveSizePolicy -XX:MaxPermSize=512M -XX:SurvivorRatio=3 -XX:NewRatio=1 ${APP_NAME}.jar >${APP_NAME}.log 2>&1 & 28 fi 29 } 30 31 #停止方法 32 stop(){ 33 is_exist 34 if [ $? -eq "0" ]; then 35 kill -9 $pid 36 else 37 echo "${APP_NAME} is not running" 38 fi 39 } 40 41 #输出运行状态 42 status(){ 43 is_exist 44 if [ $? -eq "0" ]; then 45 echo "${APP_NAME} is running. Pid is ${pid}" 46 else 47 echo "${APP_NAME} is NOT running." 48 fi 49 } 50 51 #重启 52 restart() { 53 stop 54 start 55 } 56 57 #根据输入参数,选择执行对应方法,不输入则执行使用说明 58 case "$1" in 59 "start") 60 start 61 ;; 62 "stop") 63 stop 64 ;; 65 "status") 66 status 67 ;; 68 "restart") 69 restart 70 ;; 71 *) 72 usage 73 ;; 74 esac