使用systemctl保证springboot jar应用后台运行

svbadmin学习日志

本学习日志是使用Springboot和Vue来搭建的后台管理系统:
演示地址:http://118.31.68.110:8081/index.html
账号:root
密码:123
所有代码可以在gitbub上找到,切换到相应分支即可。【代码传送门

正篇

第一节 spring boot 模块化构建项目
第二节 整合mybatisplus完成用户增删改查
第三节 整合springsecurity实现基于RBAC的用户登录
第四节 springsecurity结合jwt实现前后端分离开发
第五节 使用ResponseBodyAdvice格式化接口输出
第六节 springboot结合redis实现缓存策略
第七节 springboot结合rabbitmq实现队列消息
第八节 springboot结合rabbitmq实现异步邮件发送
第九节 利用springboot的aop实现行为日志管理
第十节 利用Quartz实现数据库定时备份
第十一节 springboot配置log输出到本地文件
第十二节 使用flyway对数据库进行版本管理
第十三节 springboot配合VbenAdmin实现前端登录
第十四节 springboot配合VbenAdmin实现用户CURD
第十五节 基于RBAC的权限管理VbenAdmin前端实现
第十六节 springboot 打包vue代码实现前后端统一部署

番外

2.1 数据库设计原则
3.1 配置apifox自动获取登录的token
13.1 springboot 全局捕捉filter中的异常
14.1 springsecurity整合mybatisplus出现isEnable的问题和解决方案
16.1 使用systemctl保证springboot jar应用后台运行


前言

使用java -jar 启动项目后,必须保证终端一直开着才行,当然可以用nohup来进行后台运行,但毕竟不是很优雅。
这里我们采用systemctl来保证开机后台运行。


一、配置system service

1.上传jar

上传到/var/app

2.创建tomcat启动用户并授权

输入命令 getent group tomcat || groupadd -r tomcat
输入命令 getent passwd tomcat || useradd -r -d /opt -s /bin/nologin -g tomcat tomcat

3.更改文件权限

输入命令 chown -R tomcat:tomcat /var/app

4.配置开机启动脚本

在/usr/lib/systemd/system目录下增加sbvadmin.service,目录必须是绝对目录
输入命令sudo vim /usr/lib/systemd/system/sbvadmin.service

[Unit]
Description=sbvadmin
After=syslog.target

[Service]
User=tomcat
Group=tomcat

ExecStart=/usr/bin/java -Xms512M -Xmx512M -jar /var/app/sbvadmin.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

5.保存刷新

保存配置文件后执行systemctl daemon-reload命令刷新
输入命令 systemctl daemon-reload

6.开启

systemctl enable sbvadmin 配置开机启动
systemctl start sbvadmin 启动
systemctl stop sbvadmin 停止
systemctl restart sbvadmin 重启
systemctl disable sbvadmin 删除开机启动
systemctl status sbvadmin 查看启动状态
systemctl daemon-reload 刷新

二、测试

如果代码更新,可以重新上传jar,然后执行

systemctl restart sbvadmin

运行成功后,会自动生成logs文件夹
在这里插入图片描述


总结

  1. 日志路径需要动态指定,不然使用systemctl是无法创建的

问题

  1. Failed to create parent directories for [/./logs/sbvadmin-error.log]
    创建一个监听类实现动态指定日志目录
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.spi.ContextAwareBase;
import ch.qos.logback.core.spi.LifeCycle;
import org.springframework.boot.system.ApplicationHome;

import java.io.File;

public class CustomLogContextListener extends ContextAwareBase implements LoggerContextListener, LifeCycle {

    /** 存储日志路径标识 */
    public static final String LOG_PAHT_KEY = "LOG_HOME";

    @Override
    public void start() {
        //获取当前jar 的执行路径
        ApplicationHome home = new ApplicationHome(getClass());
        File jarFile = null;
        try{
            jarFile = home.getSource();
            String parent = jarFile.getParent();
            System.out.println(parent);

            String path = jarFile.getParentFile().toString();
            System.out.println("CustomLogContextListener_start_filePath:"+path);
            System.setProperty(LOG_PAHT_KEY, path + File.separator + "logs");
            Context context = getContext();
            context.putProperty(LOG_PAHT_KEY,  path + File.separator + "logs");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public boolean isResetResistant() {
        return false;
    }

    @Override
    public void onStart(LoggerContext context) {

    }

    @Override
    public void onReset(LoggerContext context) {

    }

    @Override
    public void onStop(LoggerContext context) {

    }

    @Override
    public void onLevelChange(Logger logger, Level level) {

    }

    @Override
    public void stop() {

    }

    @Override
    public boolean isStarted() {
        return false;
    }
}

logback-spring.xml中加入监听类

 <!-- 将监听类加入到xml -->
 <contextListener class="com.shenfangtao.config.CustomLogContextListener" />

参考文档:
Spring Boot版轻论坛系统在服务器上部署
springboot 动态指定日志路径(logback) 自动跟随项目路径

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

F_angT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值