LoginSignup
1
0

More than 1 year has passed since last update.

Spring-Bootアプリをパソコンを切ってもEC2上で起動させておく手順(デーモン化)

Last updated at Posted at 2021-05-15

configurationタグの箇所3行を記入したjarファイルを作ります。これにより後述のsystemd enableとstartが実行可能になります。

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


ここからの手順はHogeをご自身のアプリ名に読み替えて下さい。以降EC2上での実行コマンドです。

sudo vi /etc/systemd/system/Hoge-0.0.1-SNAPSHOT.service



vimでUnitを作成します。vimの編集の際には一度aを押してINSERTと表示されてる状態にし下記スクショの内容を記入、作成完了後はEscキーを押下し:wqと入力して保存・終了します。
[Unit]
Description = 任意のアプリ説明
After=syslog.target

[Service]
ExecStart = /home/ec2-user/Hoge-0.0.1-SNAPSHOT.jar
Restart = no
Type = simple
User = ec2-user
Group = ec2-user
SuccessExitStatus = 143

[Install]
WantedBy = multi-user.target
sudo systemctl daemon-reload
sudo chmod 744 Hoge-0.0.1-SNAPSHOT.jar
sudo systemctl enable Hoge-0.0.1-SNAPSHOT.service 
sudo systemctl start Hoge-0.0.1-SNAPSHOT.service 


jarファイルの再デプロイは下記の順番で行います。

sudo systemctl disable Hoge-0.0.1-SNAPSHOT.service 
sudo systemctl stop Hoge-0.0.1-SNAPSHOT.service 

EC2jar削除
EC2新jar追加
reload(すでに記述済み)
chmod(すでに記述済み)
start(すでに記述済み)
enable(すでに記述済み)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0