ubuntu에서 systemd service 생성하기

ubuntu에서 systemd service 생성하기

  1. 서비스 명세 작성 $ sudo vi /etc/systemd/system/apiServer.service ==>
[Unit]
Description=ApiServer
After=multi-user.target

[Service]
Type=forking
ExecStart=/usr/bin/go run main.go
WorkingDirectory=/home/ubuntu/apiServier

Restart=always
RestartSec=10
KillSignal=SIGINT

SyslogIdentifier=api-server
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  1. 권한부여 $ sudo chmod 644 /etc/systemd/system/apiServier.service

  2. systemd 데몬 reload $ sudo systemctl daemon-reload

  3. 실행 $ sudo systemctl start apiServer.service

  4. 확인

ubuntu@ip-172-31-46-221:~$ systemctl status apiServer.service
● apiServer.service - ApiServer
     Loaded: loaded (/etc/systemd/system/apiServer.service; enabled; vendor preset: enabled)
     Active: activating (start) since Tue 2022-08-16 18:18:23 UTC; 30s ago
Cntrl PID: 7322 (coding-test-inv)
      Tasks: 5 (limit: 1146)
     Memory: 1.8M
        CPU: 28ms
     CGroup: /system.slice/apiServer.service
             └─7322 /home/ubuntu/apiServer/main.go

Aug 16 18:18:23 ip-172-31-46-221 systemd[1]: apiServer.service: Scheduled restart job, restart counter is at 16.
Aug 16 18:18:23 ip-172-31-46-221 systemd[1]: Stopped ApiServer.
Aug 16 18:18:23 ip-172-31-46-221 systemd[1]: Starting ApiServer...