<systemctl로 서비스 예제>
먼저 httpd를 설치한다.
$ sudo yum install httpd
(참고로 예시로 사용할 crond는 이미 설치되어 있고 실행중인 데몬이다.)
systemctl 데몬 관련 커맨드
* systemctl start : 데몬 시작
* systemctl stop : 데몬 종료
* systemctl restart : 데몬 재시작
* systemctl reload : 데몬 reload (예, apache graceful )
* systemctl status : 데몬 상태 확인
* systemctl enable : 활성화 - 부팅 이후에도 계속 데몬을 실행할지
* systemctl disable : 활성화 - 부팅 이후에도 계속 데몬을 실행안하도록
* systemctl is-enabled : 활성화인지 확인한다.
* systemctl is-active : 동작 중인지 확인한다.
예시
$ sudo systemctl start httpd.service
$ sudo systemctl status httpd.service
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
Active: active (running) since 수 2016-05-11 17:58:17 KST; 4s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 11784 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─11784 /usr/sbin/httpd -DFOREGROUND
├─11785 /usr/sbin/httpd -DFOREGROUND
├─11786 /usr/sbin/httpd -DFOREGROUND
├─11788 /usr/sbin/httpd -DFOREGROUND
├─11791 /usr/sbin/httpd -DFOREGROUND
└─11792 /usr/sbin/httpd -DFOREGROUND
$ sudo systemctl enable httpd.service
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
$ sudo systemctl status httpd.service
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active: active (running) since 수 2016-05-11 17:58:17 KST; 21s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 11784 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─11784 /usr/sbin/httpd -DFOREGROUND
├─11785 /usr/sbin/httpd -DFOREGROUND
├─11786 /usr/sbin/httpd -DFOREGROUND
├─11788 /usr/sbin/httpd -DFOREGROUND
├─11791 /usr/sbin/httpd -DFOREGROUND
└─11792 /usr/sbin/httpd -DFOREGROUND
$ sudo systemctl restart httpd.service
$ sudo systemctl status httpd.service
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active: active (running) since 수 2016-05-11 17:58:53 KST; 2s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 11831 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Main PID: 11836 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─11836 /usr/sbin/httpd -DFOREGROUND
├─11837 /usr/sbin/httpd -DFOREGROUND
├─11838 /usr/sbin/httpd -DFOREGROUND
├─11839 /usr/sbin/httpd -DFOREGROUND
├─11840 /usr/sbin/httpd -DFOREGROUND
└─11843 /usr/sbin/httpd -DFOREGROUND
$ sudo systemctl reload httpd.service
$ sudo systemctl status httpd.service
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active: active (running) since 수 2016-05-11 17:58:53 KST; 20s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 11831 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 11859 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Main PID: 11836 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─11836 /usr/sbin/httpd -DFOREGROUND
├─11860 /usr/sbin/httpd -DFOREGROUND
├─11861 /usr/sbin/httpd -DFOREGROUND
├─11862 /usr/sbin/httpd -DFOREGROUND
├─11863 /usr/sbin/httpd -DFOREGROUND
└─11864 /usr/sbin/httpd -DFOREGROUND
$ sudo systemctl stop httpd.service
$ sudo systemctl status httpd.service
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
Active: inactive (dead) since 수 2016-05-11 17:59:22 KST; 1s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 11883 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 11859 ExecReload=/usr/sbin/httpd $OPTIONS -k graceful (code=exited, status=0/SUCCESS)
Process: 11836 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
Main PID: 11836 (code=exited, status=0/SUCCESS)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
$ sudo systemctl disable httpd.service
rm '/etc/systemd/system/multi-user.target.wants/httpd.service'
$ systemctl is-enabled httpd.service
disabled
$ systemctl is-active httpd.service
unknown
$ systemctl is-active crond.service
inactive
$ systemctl is-enabled crond.service
enabled
서비스 설정 파일 확인하기
$ ls -al /usr/lib/systemd/system/httpd.service
-rw-r--r-- 1 root root 752 11월 20 06:35 /usr/lib/systemd/system/httpd.service
ls -al /usr/lib/systemd/system/crond.service
-rw-r--r--. 1 root root 284 7월 31 2014 /usr/lib/systemd/system/crond.service
<systemctl 커맨드 맛보기>
서비스 타입중 현재 실행중인 시스템 서비스 목록 보기
$ systemctl -t service -a --state running
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active running Arbitrary Executable File Formats File System Automount Point
session-30.scope loaded active running Session 30 of user deploy
acpid.service loaded active running ACPI Event Daemon
auditd.service loaded active running Security Auditing Service
dbus.service loaded active running D-Bus System Message Bus
getty@tty1.service loaded active running Getty on tty1
irqbalance.service loaded active running irqbalance daemon
network.service loaded active running LSB: Bring up/down networking
ntpd.service loaded active running Network Time Service
rsyncd.service loaded active running fast remote file copy program daemon
rsyslog.service loaded active running System Logging Service
snmpd.service loaded active running Simple Network Management Protocol (SNMP) Daemon.
sshd.service loaded active running OpenSSH server daemon
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-udevd.service loaded active running udev Kernel Device Manager
dbus.socket loaded active running D-Bus System Message Bus Socket
systemd-journald.socket loaded active running Journal Socket
systemd-udevd-control.socket loaded active running udev Control Socket
systemd-udevd-kernel.socket loaded active running udev Kernel Socket
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
20 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.
시스템의 모든 서비스의 간단한 모든 상태 정보를 보고 싶다면, 다음을 사용한다.
$ systemctl --type service --all
UNIT LOAD ACTIVE SUB DESCRIPTION
acpid.service loaded active running ACPI Event Daemon
auditd.service loaded active running Security Auditing Service
brandbot.service loaded inactive dead Flexible Branding Service
cloud-config.service loaded active exited Apply the settings specified in cloud-config
cloud-final.service loaded active exited Execute cloud user/final scripts
cloud-init-local.service loaded active exited Initial cloud-init job (pre-networking)
cloud-init.service loaded active exited Initial cloud-init job (metadata service crawler)
crond.service loaded inactive dead Command Scheduler
dbus.service loaded active running D-Bus System Message Bus
display-manager.service not-found inactive dead display-manager.service
dracut-shutdown.service loaded inactive dead Restore /run/initramfs
emergency.service loaded inactive dead Emergency Shell
exim.service not-found inactive dead exim.service
getty@tty1.service loaded active running Getty on tty1
ip6tables.service not-found inactive dead ip6tables.service
iptables.service not-found inactive dead iptables.service
유닛 파일 타입의 systemctl로 사용할 수 있는 시스템 서비스 목록을 본다.
$ systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
brandbot.path disabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-2.scope static
session-29.scope static
session-30.scope static
session-9.scope static
acpid.service enabled
arp-ethers.service disabled
atd.service disabled
auditd.service enabled
autovt@.service disabled
brandbot.service static
cloud-config.service enabled
cloud-final.service enabled
cloud-init-local.service enabled
cloud-init.service enabled
...
리스닝하는 소켓 타입의 systemctl로 사용할 수 있는 시스템 서비스 목록을 본다.
$ systemctl list-sockets
LISTEN UNIT ACTIVATES
/dev/initctl systemd-initctl.socket systemd-initctl.service
/dev/log systemd-journald.socket systemd-journald.service
/run/systemd/journal/socket systemd-journald.socket systemd-journald.service
/run/systemd/journal/stdout systemd-journald.socket systemd-journald.service
/run/systemd/shutdownd systemd-shutdownd.socket systemd-shutdownd.service
/run/udev/control systemd-udevd-control.socket systemd-udevd.service
/var/run/dbus/system_bus_socket dbus.socket dbus.service
kobject-uevent 1 systemd-udevd-kernel.socket systemd-udevd.service
8 sockets listed.
Pass --all to see loaded but inactive sockets, too.
사용할 수 있는 모든 유닛 타입은 다음과 같다.
$ systemctl -t help
Available unit types:
service
socket
target
device
mount
automount
snapshot
timer
swap
path
slice
scope
특정 서비스의 dependency 유닛을 보려면 다음과 같은 커맨드를 사용한다.
$ systemctl list-dependencies crond.service
crond.service
├─system.slice
└─basic.target
├─microcode.service
├─rhel-autorelabel-mark.service
├─rhel-autorelabel.service
├─rhel-configure.service
├─rhel-dmesg.service
├─rhel-loadmodules.service
├─paths.target
├─slices.target
│ ├─-.slice
│ └─system.slice
├─sockets.target
│ ├─dbus.socket
│ ├─rsyncd.socket
│ ├─systemd-initctl.socket
│ ├─systemd-journald.socket
│ ├─systemd-shutdownd.socket
│ ├─systemd-udevd-control.socket
│ └─systemd-udevd-kernel.socket
├─sysinit.target
│ ├─dev-hugepages.mount
│ ├─dev-mqueue.mount
│ ├─kmod-static-nodes.service
│ ├─plymouth-read-write.service
│ ├─plymouth-start.service
│ ├─proc-sys-fs-binfmt_misc.automount
│ ├─sys-fs-fuse-connections.mount
│ ├─sys-kernel-config.mount
│ ├─sys-kernel-debug.mount
│ ├─systemd-ask-password-console.path
│ ├─systemd-binfmt.service
│ ├─systemd-journal-flush.service
│ ├─systemd-journald.service
│ ├─systemd-modules-load.service
│ ├─systemd-random-seed.service
│ ├─systemd-sysctl.service
│ ├─systemd-tmpfiles-setup-dev.service
│ ├─systemd-tmpfiles-setup.service
│ ├─systemd-udev-trigger.service
│ ├─systemd-udevd.service
│ ├─systemd-update-utmp.service
│ ├─systemd-vconsole-setup.service
│ ├─cryptsetup.target
│ ├─local-fs.target
│ │ ├─-.mount
│ │ ├─rhel-import-state.service
│ │ ├─rhel-readonly.service
│ │ ├─systemd-fsck-root.service
│ │ └─systemd-remount-fs.service
│ └─swap.target
│ └─swap-swapfile1.swap
└─timers.target
└─systemd-tmpfiles-clean.timer