記一次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

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