[리눅스] systemd 서비스등록, 자주 쓰는 systemctl 커맨드


systemd에 서비스를 등록하는 방법과 서비스를 컨트롤할 때 주로 사용하는 systemctl 커맨드를 정리해본다.

systemctl : systemd 상태를 조회하고 컨트롤하는 유틸리티

systemd 서비스 등록

등록 위치 : /etc/systemd/system

vi 로 {service name}.service 파일 생성 후 아래 내용 저장

$vi /etc/systemd/system/{service name}.service

#아래 내용 저장
------------------------------------------
[Unit]
Description={service 설명}

[Service]
Type=simple
User=root
ExecStart=/usr/bin/python3 {service.py}  # => 서비스 실행 command
RestartSec=10s
Restart=on-failure

[Install]
WantedBy=multi-user.target

systemd 상태 조회

# 로딩된 전체 유닛 목록 조회
$systemctl

# 활성화된(active) 서비스 조회
$systemctl list-units --type service

# 전체 서비스 조회
$systemctl list-units --type service -all

# 전체 서비스 상태(state) 조회
$systemctl list-unit-files --type service

systemd 서비스 컨트롤

# 서비스 시작
$systemctl start {service name}

# 서비스 종료
$systemctl stop {service name}

# 서비스 상태 조회 (active / inactive / enable / disable 등 확인)
$systemctl status {service name}

# 서비스  재시작 (start -> stop )
$systemctl restart {service name}

# 서비스 리로딩 (서비스를 종료하지 않고 설정 값 반영)
$systemctl reload {service name}

# 시스템 부팅 시 서비스 자동 실행되도록 등록
$systemctl enable {service name}

# 서비스 자동실행 등록 해제
$systemctl disable {service name}





© 2021. by tytybro

Powered by tytybro