jar 包通用自启脚本

jar 包通用自启脚本

  1. vim /etc/init.d/xxxx
#!/bin/bash
# chkconfig: 2345 85 15
# description:auto_run

# 工作目录修改成自己的地址
# 注意!!! 并且该目录中只能存在一个jar包
# 如需自定义,将*.jar 替换为自定义名
APP_HOME=/usr/local/xxx
JAR_HOME_TOW="`cd ${APP_HOME} && find -name '*.jar' `"

APP_NAME=${JAR_HOME_TOW:2}

cd $APP_HOME

#检查程序是否在运行
is_exist(){
  pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
  #如果不存在返回1,存在返回0     
  if [ -z "${pid}" ]; then
   return 1
  else
    return 0
  fi
}
 
#启动方法
start(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "================================================="
    echo "warn: $APP_NAME is already running. (pid=$pid)"
    echo "================================================="
  else
    nohup java -jar $APP_NAME  > /dev/null 2>&1 &
    echo "${APP_NAME} start success"
  fi
}
 
#停止方法
stop(){
  is_exist
  if [ $? -eq "0" ]; then
    kill -9 $pid
	echo "${APP_NAME} stop success"
  else
	echo "================================================="
    echo "warn: $APP_NAME is not running"
    echo "================================================="
  fi  
}
 
#输出运行状态
status(){
  is_exist
  if [ $? -eq "0" ]; then
	echo "================================================="
    echo "warn: $APP_NAME is already running. (pid=$pid)"
    echo "================================================="
  else
    echo "================================================="
    echo "warn: $APP_NAME is not running"
    echo "================================================="
  fi
}

#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
  "start")
    start
    ;;
  "stop")
    stop
    ;;
  "status")
    status
    ;;
  "restart")
    stop
	echo "restart the application ..."
    start
    ;;
  *)
	echo "================================================="
    echo "Tips: start|stop|restart|status"
    echo "================================================="
    ;;
esac

  1. 添加执行权限

    chmod +x /etc/init.d/xxx
    
  2. 开机自启

    chkconfig xxx on
    
posted @ 2022-02-21 00:20  那个吧..你懂的!  阅读(98)  评论(0编辑  收藏  举报