Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

Keviny Blog

CentOS7 에서 Systemd로 서비스 등록하기 (+ systemctl vs. service) 본문

시스템 관리자

CentOS7 에서 Systemd로 서비스 등록하기 (+ systemctl vs. service)

Keviny 2019. 12. 4. 15:13

CentOS 7부터는 이전에 사용하던 SysV(init system) 대신하여, systemd 을 system & service manager 로 사용합니다.

Systemd is a system and service manager for Linux operating systems. It is designed to be backwards compatible with SysV init scripts, and provides a number of features such as parallel startup of system services at boot time, on-demand activation of daemons, or dependency-based service control logic. In Red Hat Enterprise Linux 7, systemd replaces Upstart as the default init system.

#ref. https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/chap-managing_services_with_systemd


systemctl 을 systemd 를 컨트롤하는 cli 명령어이다.

그리고 CentOS 7 에서 service 명령어를 사용해보면

Redirecting to /bin/systemctl ...

라고 나오면서 systemctl 로 리다이렉팅 되는 걸 볼 수 있다.

 

하지만 여기서 실수할 수 있는 부분이, 구글링으로 service 관련해서 SysV init script 를 만들다보면 systemctl 과 service 가 각각 따로 노는 기이한 현상이 발생하기도 한다.

 

예를 들어 service tomcat start 하고, service tomcat status 하면 running 중인데 ...

systemctl status tomcat 하면 inactive 로 뜨는 상황이 발생하는 것이다...

 

그러다가 알게 된 것이 위에 얘기했던 것처럼 CentOS7 부터는 systemctl 을 기본 매니저로 사용한다는 것!

 

그리고 service 를 부팅 시에 자동으로 start 하게 하기 위해, /etc/init.d/ 에 script 를 적용하는 경우가 있는데...

이것도 systemd 를 이용하여 service 를 등록하고 target 을 정해놓으면 SysV init script 처럼 특정 런레벨에 동작하게 할 수 있다. 아래는 SysV 의 Runlevel 과 systemd 의 target unit 을 비교한 표다.

Runlevel (SysV)

Target Units

Description

0

runlevel0.target, poweroff.target

Shut down and power off the system.

1

runlevel1.target, rescue.target

Set up a rescue shell.

2

runlevel2.target, multi-user.target

Set up a non-graphical multi-user system.

3

runlevel3.target, multi-user.target

Set up a non-graphical multi-user system.

4

runlevel4.target, multi-user.target

Set up a non-graphical multi-user system.

5

runlevel5.target, graphical.target

Set up a graphical multi-user system.

6

runlevel6.target, reboot.target

Shut down and reboot the system.

#ref. https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-Managing_Services_with_systemd-Targets

 

systemd 에서 service 를 등록하려면 일반적으로 Custom Unit File 을 만들게 된다.

10.6.2. Creating Custom Unit Files

Create a unit file in the /etc/systemd/system/ directory and make sure it has correct file permissions. Execute as root:

#ref. https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/sect-managing_services_with_systemd-unit_files

 

아래는 내가 tomcat 서비스를 systemd 에 서비스로 등록할 때 참고했던 사이트에서 발췌한 내용이다.

# Systemd unit file for tomcat

[Unit]

Description=Apache Tomcat Web Application Container

After=syslog.target network.target

 

[Service]

Type=forking

Environment="JAVA_HOME=/usr/java/jdk-10.0.1/"

Environment="CATALINA_PID=/usr/local/tomcat/temp/tomcat.pid"

Environment="CATALINA_HOME=/usr/local/tomcat"

Environment="CATALINA_BASE=/usr/local/tomcat"

Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"

ExecStart=/usr/local/tomcat/bin/startup.sh

ExecStop=/usr/local/tomcat/bin/shutdown.sh

User=tomcat

Group=tomcat

UMask=0007

RestartSec=10

Restart=always

 

[Install]

WantedBy=multi-user.target

#ref. https://suwoni-codelab.com/linux/2018/05/21/tomcat-installation/

 

systemd를 이용하여 tomcat 을 multi instance로 구현한 내용은 아래 글에 정리해두었다.

#ref. https://keviny.tistory.com/2

 

 

 

요약.

1. CentOS 7 에서는 service 명령어는 사용하지 말고, /etc/init~ 으로 시작하는 폴더에 script를 작성하지 말자.

2. CentOS 7 에서 custom service 를 생성하고, init script를 작성하고 싶다면 systemd 용 custom unit file 을 작성하자.

 

참고자료.

1. Unit file 작성 시에 참고할만한 사이트 : https://fmd1225.tistory.com/93

 

끝.