J.BF Story

[Ubuntu] 서비스 등록 (systemd) 본문

Server/Linux

[Ubuntu] 서비스 등록 (systemd)

J.BF 2022. 7. 27. 23:38
Ubuntu 18.04

 

1. 서비스 파일 생성

/etc/systemd/system/test.service

[Unit]
Description=Test Service
After=multi-user.target

[Service]
Type=simple
User=testuser
Group=testuser
WorkingDirectory=/home/testuser/program
ExecStart=/home/testuser/anaconda3/bin/python run.py
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • User, Group / WorkingDirectory
    • [옵션] 프로그램 실행 유저 / 프로그램 작업 디렉터리
    • ExecStart 앞에 위치해야 설정이 적용됨
  • Restart=on-failure: 서비스 종료 시 자동 재시작
  • 필요 시 'Environment'로 환경변수를 설정할 수 있음

 

2. 서비스 등록

systemctl daemon-reload
# 서버 재부팅 시에 서비스 자동 시작
systemctl enable test.service
# 서비스 시작
systemctl start test.service

 

3. 서비스 확인

systemctl status test.service

** 에러 메세지 확인

journalctl -xe -u test.service
Comments