SprngBoot应用在linux作为服务启动

本文详细介绍了如何将SpringBoot应用部署为Linux服务,包括编写init.d脚本、设置权限、开机自启等步骤,并提供了常见问题解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.youkuaiyun.com/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.youkuaiyun.com/ly690226302/article/details/79260875

https://blog.youkuaiyun.com/fwk19840301/article/details/80065723

https://blog.youkuaiyun.com/paulangsky/article/details/51321707

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值