SRE/Linux Basics

Register Service, 서비스 등

게임이 더 좋아 2023. 1. 4. 23:02
반응형
728x170

서비스 등록 과정

service 파일 생성

cd /etc/systemd/system

sudo vi my-service.service
[Unit]
Description=MyService

[Service]
Type=simple
WorkingDirectory=/usr/share/MyService
ExecStart=/usr/share/MyService/bin/MyService -f /path/to/MyService.yml
Restart=on-failure

[Install]
WantedBy=multi-user.target

systemctl로 서비스 등록

#systemd Reload
sudo systemctl daemon-reload

#startup service로 등록
sudo systemctl enable my-service

#실행
sudo systemctl start logstash
#정지
sudo systemctl stop logstash

 

Restart해서 테스트

 


 

.service 파일 

 

이 파일에 정해진 포맷은 없고 필요하다면 더 집어넣을 수 있음

 

일반적으로 3가지 Section이 존재

  • [Unit]: This section contains metadata about the service, such as its name and a description.
  • [Service]: This section contains the configuration for the service itself, including the command that is used to start it and the conditions under which it should be restarted.
  • [Install]: This section contains information about how the service should be installed, such as which target it should be installed to.
[Unit]
Description=My Service

[Service]
Type=simple
ExecStart=/path/to/service
Restart=on-failure

[Install]
WantedBy=multi-user.target

주요한 Option

 
UNIT
Description A short description of the unit. => 설명
Documentation A list of URIs referencing documentation.
Before, After The order in which units are started. => 해당 유닛이 시작 되기 전(before) 또는 후(after)에 이 서비스가 실행됨
Requires If this unit gets activated, the units listed here will be activated as well. If one of the other units gets deactivated or fails, this unit will be deactivated. => 상위 서비스의 의존성 구성 (충족시켜야 서비스 시작 가능)
Wants Configures weaker dependencies than Requires. If any of the listed units does not start successfully, it has no impact on the unit activation. This is the recommended way to establish custom unit dependencies. => 서비스 종속성(Require 과 유사)
Conflicts If a unit has a Conflicts setting on another unit, starting the former will stop the latter and vice versa.

 

 

SERVICE
Type Configures the process start-up type. One of:
simple (default) – starts the service immediately. It is expected that the main process of the service is defined in ExecStart.
forking – considers the service started up once the process forks and the parent has exited.
oneshot – similar to simple, but it is expected that the process has to exit before systemd starts follow-up units (useful for scripts that do a single job and then exit). You may want to set RemainAfterExit=yes as well so that systemd still considers the service as active after the process has exited.
dbus – similar to simple, but considers the service started up when the main process gains a D-Bus name.
notify – similar to simple, but considers the service started up only after it sends a special signal to systemd.
idle – similar to simple, but the actual execution of the service binary is delayed until all jobs are finished.
Environment Sets environment variables for executed processes. Each line is unquoted using the rules described in "Quoting" section in systemd.syntax(7) and becomes a list of variable assignments. If you need to assign a value containing spaces or the equals sign to a variable, put quotes around the whole assignment. Variable expansion is not performed inside the strings and the "$" character has no special meaning. Specifier expansion is performed, see the "Specifiers" section in systemd.unit(5).
This option may be specified more than once, in which case all listed variables will be set. If the same variable is listed twice, the later setting will override the earlier setting. If the empty string is assigned to this option, the list of environment variables is reset, all prior assignments have no effect.
The names of the variables can contain ASCII letters, digits, and the underscore character. Variable names cannot be empty or start with a digit. In variable values, most characters are allowed, but non-printable characters are currently rejected.
=> 사용할 환경 변수 설정 반드시 Exec 옵션보다 앞에 위치해야 함
WorkingDirectory Takes a directory path relative to the service's root directory specified by RootDirectory=, or the special value "~".
Sets the working directory for executed processes.
If set to "~", the home directory of the user specified in User= is used.
If not set, defaults to the root directory when systemd is running as a system instance and the respective user's home directory if run as user.
If the setting is prefixed with the "-" character, a missing working directory is not considered fatal.
If RootDirectory=/RootImage= is not set, then WorkingDirectory= is relative to the root of the system running the service manager. 
=> 서비스가 구동 후에 어디서 작업되는 지에 대한 디렉토리 설정, 서비스의 working directory
ExecStart Commands with arguments to execute when the service is started. Type=oneshot enables specifying multiple custom commands that are then executed sequentially. ExecStartPre and ExecStartPost specify custom commands to be executed before and after ExecStart.
=> 서비스 시작 시에 실행되는 Command
ExecStop Commands to execute to stop the service started via ExecStart.
ExecReload Commands to execute to trigger a configuration reload in the service.
Restart With this option enabled, the service shall be restarted when the service process exits, is killed, or a timeout is reached with the exception of a normal stop by the systemctl stop command. => 해당 커맨드로 stop 하지 않으면 다시 Auto Restart 됨
RemainAfterExit If set to True, the service is considered active even when all its processes exited. Useful with Type=oneshot. Default value is False.

 

 

INSTALL
Alias A space-separated list of additional names for the unit. Most systemctl commands, excluding systemctl enable, can use aliases instead of the actual unit name.
RequiredBy, WantedBy The current service will be started when the listed services are started. See the description of Wants and Requires in the [Unit] section for details.
=> 어떤 서비스 이후에 시작될지 결정 → 위의 Wants와 관계있음
Also Specifies a list of units to be enabled or disabled along with this unit when a user runs systemctl enable or systemctl disable.

 


 

참고 링크

https://fmd1225.tistory.com/93

https://www.freedesktop.org/software/systemd/man/systemd.service.html

https://www.shellhacks.com/systemd-service-file-example/

반응형
그리드형

'SRE > Linux Basics' 카테고리의 다른 글

curl  (0) 2023.01.21
Symblic Link, Hard Link  (0) 2023.01.04
touch  (0) 2022.12.31
Access Control Lists, ACLs  (0) 2022.12.30
rmdir  (0) 2022.12.25