记一次centos7上将jar包注册为服务启动

将java程序打成jar包

在pom文件中加入以下代码:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

打包上传至服务器

设置成服务启动

假设jar包在/project下,包名叫deploy-test.jar。
在/etc/systemd/system目录下创建deploy-test.service文件,内容如下:

[Unit]
Description=deploy-test
After=syslog.target

[Service]
User=root
ExecStart=/project/deploy-test.jar -Dfile.encoding=UTF-8(编码可不加)
SuccessExitStatus=143

[Install]
WantedBy=multi-user.targeti

将jar包设置成可执行文件:chmod +x deploy-test.jar

启动:systemctl start deploy-test

查看状态:systemctl -l status deploy-test

停止:systemctl stop deploy-test

service文件改变后需要刷新下:systemctl daemon-reload

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章