Systemd

From 탱이의 잡동사니
Jump to navigation Jump to search

Overview

Linux systemd 내용 정리

Configure files

systemd configure file

systemd 는 /etc/systemd 아래에 설정파일을 두고 있다. <source lang=bash> $ ls /etc/systemd/ bootchart.conf journald.conf logind.conf network ntp-units.d resolved.conf system system.conf timesyncd.conf user user.conf </source>

service/target configure file

systemd에서 관리하는 서비스들은 service/target 파일들을 통해서 관리하는데, 다음의 위치에 저장되게 된다.

바이너리 실행파일은 아래 경로에서 확인 가능하며
$ ls /lib/systemd/

기본적인 시스템의 Service, Target은 아래에 위치하고 있다
$ ls /lib/systemd/system

Service management

서비스를 설정하고 관리하는 방법은 기본적으로 다음과 같다.

- 서비스 활성화
$ systemctl enable [서비스명]

- 서비스 비활성화
$ systemctl disable [서비스명]

- 서비스 시작
$ systemctl start [서비스명]

- 서비스 종료
$ systemctl stop [서비스명]

- 서비스 재시작
$ systemctl restart [서비스명]

- 서비스 갱신
$ systemctl reload [서비스명]

service restart

systemd 에 등록되어 있는 서비스는 서비스 내용에 따라 서비스 종료시 바로 자동으로 실행되는 경우가 있다. 이런 경우, kill -9 명령어로 서비스를 분명히 종료 했지만 바로 재시작이 되게 되는데, 어떤 이유로 해당 서비스가 재시작되는지 알기가 까다롭게 된다. 이런 경우, /var/log/syslog 메시지를 확인하면 아래의 내용과 같이 systemd 에 의해 해당 서비스가 재시작 되었음을 확인할 수 있다.

Apr  4 08:27:04 earth systemd[1]: freeswitch.service: main process exited, code=killed, status=9/KILL
Apr  4 08:27:04 earth systemd[1]: Unit freeswitch.service entered failed state.
Apr  4 08:27:04 earth freeswitch[18707]: 18708 Backgrounding.
Apr  4 08:27:06 earth freeswitch[18707]: FreeSWITCH[18707] Waiting for background process pid:18708 to be ready.....
Apr  4 08:27:06 earth freeswitch[18707]: FreeSWITCH[18707] System Ready pid:18708

Service files

Service file 들은 systemd 에서 프로세스들을 실행 및 관리할 수 있도록 하는 설정 파일들이다.

Options

Type

Configures the process start-up tpe for this service unit. One of simple, exec, forking, oneshot, dbus, notify or idel

  • simple: Default value. The service manager will consider the unit started immediately after the main service process has been forked off. It is expected that the process configured with ExecStart= is the main process of the service. In this mode, if the process offers functionality to other processes on the system, its communication channels should be installed before the service is started up(e.g. sockets set up by systemd, via socket activation), as the service manager will imddediately proceed starting follow-up inits, right after creating the main service process, and before executing the service's binary. Note that this means systemctl start command lines for simple services will report success even if the service's binary cannot be invoked successfully(for example because the selected User=doesn't exist, or the service binary is missing).
  • oneshot: Similar like simple. However the service manager will considerthe unit started after the main process exists. It will then start follow-up units. RemainAfterExit= is particularly useful for this type of service. Type=oneshot is the implied default if neither Ty[e= nor ExecStart= are specified.

Restart

설정된 프로세스가 종료 되었을 경우, systemd 에서 프로세스를 재시작 할 것인지/말지를 설정한다. Default 는 no 이다.

서비스 재시작 시, StartLimitIntervalSec 과 StartLimitBurst 에 설정된 값으로 재시작을 시도한다. 설정 권장 값은 on-failure 이다.

예외적으로 RestartPreventExitStatus 에 설정된 signal 이나 exit code 및 systemctl stop 혹은 이와 상응하는 명령어로 인한 경우에는 재시작을 하지 않는다. 또한, RestartForceExitStatus 에 설정된 signal 및 exit code 로 인한 종료라면, 항상 재시작한다.

  • no: 재시작 하지 않음.
  • always: Clean exit code or signal, Unclean exit code, Unclean signal, Timeout, Watchdog
  • on-success: Clean exit code or signal
  • on-failure: Unclean exit code, Unclean signal, Timeout, Watchdog
  • on-abnormal: Unclean signal, Timeout, Watchdog
  • on-abort: Unclean signal
  • on-watchdog: Watchdog

Example

[Unit]
Description=Asterisk PBX And Telephony Daemon
After=network.target

[Service]
User=asterisk
Group=asterisk
Environment=HOME=/var/lib/asterisk
WorkingDirectory=/var/lib/asterisk
ExecStart=/usr/sbin/asterisk -f -C /etc/asterisk/asterisk.conf
ExecStop=/usr/sbin/asterisk -rx 'core stop now'
ExecReload=/usr/sbin/asterisk -rx 'core reload'
LimitNOFILE=1048576
Restart=always

[Install]
WantedBy=multi-user.target

See also