shell——启动服务脚本

 1 #!/bin/bash
 2 
 3 # Java ENV
 4 export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home
 5 export JRE_HOME=${JAVA_HOME}/jre
 6 
 7 # Apps Info
 8 # 应用存放地址
 9 APP_HOME=/Users/shanglishuai/temp/tyronesoft/applications
10 # 应用名称
11 APP_NAME=$1
12 
13 # Shell Info 
14 
15 # 使用说明,用来提示输入参数
16 usage() {
17     echo "Usage: sh boot [APP_NAME] [start|stop|restart|status]"
18     exit 1
19 }
20 
21 # 检查程序是否在运行
22 is_exist(){
23         # 获取PID
24         PID=$(ps -ef |grep ${APP_NAME} | grep -v $0 |grep -v grep |awk '{print $2}')
25         # -z "${pid}"判断pid是否存在,如果不存在返回1,存在返回0
26         if [ -z "${PID}" ]; then
27                 # 如果进程不存在返回1
28                 return 1
29         else
30                 # 进程存在返回0
31                 return 0
32         fi
33 }
34 
35 # 定义启动程序函数
36 start(){
37         is_exist
38         if [ $? -eq "0" ]; then
39                 echo "${APP_NAME} is already running, PID=${PID}"
40         else    
41                 nohup ${JRE_HOME}/bin/java -jar ${APP_HOME}/${APP_NAME} >/dev/null 2>&1 &
42                 PID=$(echo $!)
43                 echo "${APP_NAME} start success, PID=$!"
44         fi
45 }
46 
47 # 停止进程函数
48 stop(){
49         is_exist
50         if [ $? -eq "0" ]; then
51                 kill -9 ${PID}
52                 echo "${APP_NAME} process stop, PID=${PID}"
53         else    
54                 echo "There is not the process of ${APP_NAME}"
55         fi
56 }
57 
58 # 重启进程函数 
59 restart(){
60         stop
61         start
62 }
63 
64 # 查看进程状态
65 status(){
66         is_exist
67         if [ $? -eq "0" ]; then
68                 echo "${APP_NAME} is running, PID=${PID}"
69         else    
70                 echo "There is not the process of ${APP_NAME}"
71         fi
72 }
73 
74 case $2 in
75 "start")
76         start
77         ;;
78 "stop")
79         stop
80         ;;
81 "restart")
82         restart
83         ;;
84 "status")
85        status
86         ;;
87     *)
88     usage
89     ;;
90 esac
91 exit 0
原文地址:https://www.cnblogs.com/xull0651/p/13826234.html