SprngBoot应用在linux作为服务启动

1.服务名和jar包名都为application
/etc/init.d application
application服务内容:
###############################
#!/bin/bash
# chkconfig: 2345 10 90  
#服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。
# description: Start and Stop application
. /etc/init.d/functions
SERVICE_NAME="application"
RETVAL=0
PID=-1
PIDFILE=/var/run/${SERVICE_NAME}.PID

start() {
  # 首先检查PID文件是否已存在
  if [ -f ${PIDFILE} ]; then
    echo "PID file ${PIDFILE} already exists, please stop the service !"
    exit
  fi
  echo "Starting service ${SERVICE_NAME} ..."
  # >/dev/null 2>&1 表示不输出stdout和stderr
  # 最后一个 & 表示整个命令在后台执行
  cd "/home/topideal/apps/tms/application"
  java -Djava.security.egd=file:/dev/./urandom -jar application.jar --spring.cloud.config.profile=dev --spring.cloud.config.label=test >/dev/null  2>&1  &
  PID=$!  # 获取本shell启动的最后一个后台程序的进程号(PID)
  if [ -z ${PID} ]; then # 检查有没有获取到pid
    echo "Failed to get the process id, exit!"
    exit
  else
    echo "Starting successfully, whose pid is ${PID}"
  fi
  touch $PIDFILE
  echo ${PID} > ${PIDFILE}
}
stop() {
  if [ -f $PIDFILE ]; then # 检查PIDFILE是否存在
    PID=`cat ${PIDFILE}`
    if [ -z $PID ]; then # 检查PID是否存在于PIDFILE中
      echo "PIDFILE $PIDFILE is empty !"
      exit
    fi
    # 检查该进程是否存在
    if [ -z "`ps axf | grep $PID | grep -v grep`" ]; then
      echo "Process dead but pidfile exists!"
      exit
    else
      kill -9 $PID
      echo "Stopping service successfully , whose pid is $PID"
      rm -f $PIDFILE
    fi
  else
    echo "File $PIDFILE does NOT exist!"
  fi
}
restart() {
  stop
  start
}
status() {
  # 检查pid file是否存在
  if [ -f $PIDFILE ]; then
    PID=`cat $PIDFILE`
    # 检查pid file是否存在pid
    if [ -z $PID ] ; then
      echo "No effective pid but pidfile exists!"
    else
      # 检查pid对应的进程是否还存在
      if [ -z "`ps axf | grep $PID | grep -v grep`" ]; then
        echo "Process dead but pidfile exist"
      else
        echo "Running"
      fi
    fi
  else
    echo "Service not running"
  fi
}
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    restart
    ;;
  status)
    status
    ;;
  *)
    echo "Usage: application {start|stop|restart|status}"
    ;;
esac
###############################
2.授权
chmod 777 application
#开机启动需要额外配置 chkconfig --add application;chkconfig application on/off
3.开启服务
service application start
# systemctl start/restart/stop/status application(!!!注意观察打印日志是否报错:如找不到文件)
4.红色变绿色
chmod +x application.jar

参考博客:https://blog.csdn.net/zks_4826/article/details/80606036

SpringBoot官网上也有安装方式,这里补充一些链接:

https://www.jianshu.com/p/b61d3c913d15

http://www.hicode.club/2018/06/03/centos-start-sh/

https://juejin.im/post/5b8d2533e51d4538b35c327b

https://blog.csdn.net/ly690226302/article/details/79260875

https://blog.csdn.net/fwk19840301/article/details/80065723

https://blog.csdn.net/paulangsky/article/details/51321707

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值