???? ????? ?? Systemd: ????? ?????? ?????? ??? ??????? ??????? ??? ?????? ??????? ????? ?????? ??????? ??? ??? ??????? ????? [??????] ??????: ????? ????? ????????? ??????????. ??????? [??????] ???: ???? ????? 6.1 ????? ??????? 6.2 ??? ??????? 6.3 ???? ????? ??????? ?????? [?????] ???? ???????? ??? ??????? Target ????? ??? ??????? ?? ????? ??????? ??????? ???????
??????: ??? ?????
???????: 8 ???? 2016
?? ??????? ???????? ???? ???? Systemd.
??????? ????????
????? ????? ????? ???????? ?????? ??? ?????? ????????.
??????? ??????? ???? ???? Systemd? ??? ???????? ????? ???????? ??
/usr/lib/systemd/system
??? ??? ????? ??? ??????. ??? ??? ???? ?? ??? ????? ???????? ??? ????? ??????? ??? ????? ?????? (??
httpd.service
??? ???? ??????).
$ sudo systemctl enable httpd
????? ????? ????? ??
/etc/systemd/system
????? ???? ???? ??? ??????? ???? ???
/usr/lib/systemd/system
????????
httpd.service
?????. ???? ??? ??? ????? ???????
Systemd
????? ???
/etc/systemd/system
??? ??????? ?? ??????. ???? ???? ????? ??? ??? ?? ??? ??? ??????? ?????? ?? ??? ??????? ???? ????? ????? ??????? ??????? ??????.
??? ??? ??? ???????? ?? ??? ????? ???????? ??? ?????? ??? ???????? ??? ??? ????? ?????? ?? ????? ???????. ??? ??? ???? ?? ????? ???????? ????? ???? ?????
systemctl
start
?????. ??? ????? ??? ???????? ?? ??? ????? ???????? ??? ?????? ??? ???????? ??? ??? ????? ????????? ?? ????? ???????. ??? ??? ???? ?? ????? ???????? ????? ??? ?????
systemctl start
???.
x
$ sudo systemctl start httpd
??? ????? ????? ?????? ?? ???? ??? ?? ???????? ???? ??? ???????
systemctl
status
??? ???? ???? ??????.
x
$ sudo systemctl status httpd
?
httpd.service - The Apache HTTP Server
? Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled)
? Active: active (running) since 金 2014-12-05 12:18:22 JST; 7min ago
Main PID: 4349 (httpd)
? Status: "Total requests: 1; Current requests/sec: 0; Current traffic: ? 0 B/sec"
? CGroup: /system.slice/httpd.service
? ? ? ? ? ├─4349 /usr/sbin/httpd -DFOREGROUND
? ? ? ? ? ├─4350 /usr/sbin/httpd -DFOREGROUND
? ? ? ? ? ├─4351 /usr/sbin/httpd -DFOREGROUND
? ? ? ? ? ├─4352 /usr/sbin/httpd -DFOREGROUND
? ? ? ? ? ├─4353 /usr/sbin/httpd -DFOREGROUND
? ? ? ? ? └─4354 /usr/sbin/httpd -DFOREGROUND
?
12月 05 12:18:22 localhost.localdomain systemd[1]: Starting The Apache HTTP Server...
12月 05 12:18:22 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
12月 05 12:22:40 localhost.localdomain systemd[1]: Started The Apache HTTP Server.
??????? ????? ???? ?? ???.
?????? ????? ???? ?????????? ??? ??? ??? ??? ?????? ????? ??? ????? ??????. ???? ?????: ???? ??? ??? ??????? ????? ??????? ??????? ???? ?????: ?????? ??????? ????????? ??????? ?? ??????? ???? (??? httpd) ???? CGroup: ???? ???????? ??????? ??????? ???? ?????: ????? ???????
?????? ?????? ???? ???? ??????? ??? ?????.
systemctl stop
???.
xxxxxxxxxx
$ sudo systemctl stop httpd.service
有時(shí)候,該命令可能沒(méi)有響應,服務(wù)停不下來(lái)。這時(shí)候就不得不"殺進(jìn)程"了,向正在運行的進(jìn)程發(fā)出
kill
?????.
xxxxxxxxxx
$ sudo systemctl kill httpd.service
???????? ??? ???? ??? ????? ????? ????? ??????.
systemctl restart
???.
x
$ sudo systemctl restart httpd.service
??? ??? ????? ???? ?? ????? ??????? ??? ??? ??????? ????? ???. ???? ???? ???? ??? ??????? ??? ???????.
??? ???? ??????? ??? ??? ????? ??????? ???? ????? ??
/usr/lib/systemd/system
??????? ?? ???? ????? ??
/etc/systemd/system
??????. ??? ?????? ??? ??? ???????? ?????? ???? ?????? ?????.
systemctl
cat
???? ??????? ????? ???? ??? ???????? ???? ???:
sshd.service
????? ?????? ?????? ?? ????? ???? SSH? ?????? ?????????? ??????? ?? ????? ?????? ??? SSH.
xxxxxxxxxx
$ systemctl cat sshd.service
?
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
?
[Service]
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
Type=simple
KillMode=process
Restart=on-failure
RestartSec=42s
?
[Install]
WantedBy=multi-user.target
???? ???? ?? ??? ??????? ???? ??? ??? ???? ?? ???? ????? ??? ??? ?? ????? ??????? ???????.
????? ???? ????? ?? ??? ????????.
Unit
????
Description
???? ????? ????? ?????? ?????? ???????.
Documentation
????? ???? ???? ???????. ????????? ??????? ?? ????? ??????? ???????????? ???? ??? ??? ??????.
After
?????: ????? ???network.target
??sshd-keygen.service
??? ?? ????? ???sshd.service
??? ?? ??? ??????? ?????.
??????? ????? ???? ???? ???.
Before
???? ?????
sshd.service
??? ??? ??????? ??? ?? ?????. ???? ???
After
?
Before
????? ????? ??? ?????? ???????? ??? ????? ????????? ??????????.
??? ???? ??????? ????? ????? ??? ???? ??? ????? ?????? PostgreSQL ?????? ????????. ?? ??? ???????? ??? ??? ????? ??? ??? ??? ??????? ??? PostgreSQL? ??? ????? ???????? ??? PostgreSQL. ??? ???????? ????? ??? ????? PostgreSQL ??? ????? ???????? ????? ???? ???? ??????? ?????? ??? ????? ????? ??? ????? ????? ?????? ????????. ?????? ??????? ??????????? ????? ??? ???????
Wants
????? ????????
Requires
???.
Wants
???: ????sshd.service
??sshd-keygen.service
之間存在"弱依賴(lài)"關(guān)系,即如果"sshd-keygen.service"啟動(dòng)失敗或停止運行,不影響sshd.service
????? ?? ???????.
Requires
字段則表示"強依賴(lài)"關(guān)系,即如果該服務(wù)啟動(dòng)失敗或異常退出,那么
sshd.service
??? ?? ???? ?????.
??????
Wants
????? ?
Requires
?????? ????? ??? ????????? ??????????? ??? ????? ?????? ???????? ????? ??????? ??? ??????? ?? ??? ?????.
Service
??? ??? ??? ?????? ??????? ????? ?????? ??????.
?????? ?? ??????? ????? ????? ?????? ????? ???? ???? ???? ??????? ??? ????? ??
EnvironmentFile
????? ??????.
EnvironmentFile
?????: ???? ??? ?????? ?????? ?????? ???????. ????? ??? ????? ???key=value
??? ???????? ??????? ???? ????????$key
?? ?????? ?????? ??? ??? ?? ??? ??????? ??????.
??? ?????? ?????? ?? sshd ?? ?????? ????? ??
/etc/sysconfig/sshd
??? ?????? ?? ??? ??????? ??
ExecStart
。
ExecStart
?????: ????? ????? ???? ??? ?????? ??? ??? ?????.
?? ?????? ?????? ??? ???????
sshd
????? ?????? ??
/usr/sbin/sshd -D
$OPTIONS
? ????????? ????
$OPTIONS
??? ??
EnvironmentFile
??? ?????? ?????? ?????? ?????? ?????. ???? ????? ???? ?????? ???.
ExecReload
?????: ????? ?????? ??? ????? ????? ??????
ExecStop
?????: ????? ???? ??? ?????? ??? ????? ??????
ExecStartPre
?????: ??????? ??????? ??? ??? ??????
ExecStartPost
?????: ??????? ???? ??? ??????? ??? ??? ??????
ExecStopPost
?????: ??????? ???? ??? ??????? ??? ????? ??????
???? ????? ??? ?????? ?????.
xxxxxxxxxx
[Service]
ExecStart=/bin/echo execstart1
ExecStart=
ExecStart=/bin/echo execstart2
ExecStartPost=/bin/echo post1
ExecStartPost=/bin/echo post2
??? ??????? ?????? ????? ??????
ExecStart
????? ?????? ?????? ??? ???? ????? ????? ???? ?????? ???????? ??? ???.
xxxxxxxxxx
execstart2
post1
post2
???? ????? ???? ??? ???? ??????? ??? ???????.
-
),表示"抑制錯誤",即發(fā)生錯誤的時(shí)候,不影響其他命令的執行。比如,
EnvironmentFile=-/etc/sysconfig/sshd
(???? ????? "-" ??? ????? ????????)? ???? ??? ??? "??? ??".
/etc/sysconfig/sshd
????? ??? ?????? ??? ??? ????? ?? ???.
Type
????? ??? ??? ??? ???????. ????? ???? ???? ??????? ??? ???.
???? (?????? ??????????):
ExecStart
????? ??? ????? ?? ??????? ????????.forking:
ExecStart
???? ????? ?????? ??fork()
??? ??? ??????? ???? ???????? ?? ??? ?????? ????? ??????? ???????? ?????? ??????? ??????? ?? ??????? ????????.??? ???: ????? ??
simple
????? ????? ??? ????? ???? ?????? Systemd ????????? ??? ????? ??? ??? ??????? ??????.?????: ????? ??
simple
????? ?????? ????? D-Bus ?????.?????: ????? ??
simple
??? ?????? ???????? ???? ????? ????? ?????? ?? ???? Systemd ?????? ??????? ??????.????? ??
simple
??? ??? ???????? ??? ??? ????? ?????? ?????? ??????? ??? ??? ??? ??????. ???? ??????????? ?? ????? ??? ?????? ?????? ??? ?????? ?? ?????? ??????? ??????.
xxxxxxxxxx
[Unit]
Description=Switch-off Touchpad
?
[Service]
Type=oneshot
ExecStart=/usr/bin/touchpad-off
?
[Install]
WantedBy=multi-user.target
??? ??????? ?? ??? ??????? ????? ?? ?????? ???
oneshot
??? ??? ??? ?? ??? ?????? ????? ??? ??????? ??? ????? ???? ??? ????? ??? ??????? ????? ?????. ??? ?? ???????? ????? ?? ????? ??? ???? ?? ????????? ??? ????? ??? ??????? ??? ???.
xxxxxxxxxx
[Unit]
Description=Switch-off Touchpad
?
[Service]
Type=oneshot
ExecStart=/usr/bin/touchpad-off start
ExecStop=/usr/bin/touchpad-off stop
RemainAfterExit=yes
?
[Install]
WantedBy=multi-user.target
?? ??? ??????? ??????
RemainAfterExit
?? ????? ????? ???
yes
??? ???? ???????? ???? ?????? ?????? ?? ???????. ???? ???????? ????? ???????
systemctl
stop
??? ???? ???????
ExecStop
???? ????? ????? ??????? ??? ???? ????? ???? ?????.
Service
????? ?????? ??? ??? ?????? ???? ???? ???? ????? ???????.
KillMode
?????: ????? ????? ????? ???? sshd ?? Systemd.
?? ?????? ??????將
KillMode
??? ??
process
? ??? ???? ??? ???? ????? ??????? ???????? ??? ??? ????? ?? ?? ???????? ??????? ?? sshd? ?? ?? ????? SSH ???? ?????? ???????? ??????? ???? ?????. ??? ??????? ??? ??????? ????? ??? ?? sshd? ???? ??? ????? ??????? ????? ????? ?????? ????? SSH ???? ?????? ?????.
KillMode
???? ????? ????? ????? ??? ???.
?????? ?????? (?????? ??????????): ???? ??? ???? ???????? ??????? ???? ?????? ?????? ???????.
???????: ??? ??????? ???????? ???
??????? ??????? ?????? ????? SIGTERM? ????? ?????? ???????? ??????? ????? SIGKILL.
?? ???? ?????? ??????? ??? ??? ????? ??? ????? ??????.
?????? ??
Restart
???.
Restart
?????: ???? ????? ????? ????? Systemd ??? ???? sshd.
?? ?????? ??????
Restart
??? ??
on-failure
? ??? ??? ?? ??? ??? ?????? ???? ????? ????? sshd. ??? ????? sshd ???? ????? (??? ?????
systemctl
stop
?????) ? ??? ???? ???????.
Restart
???? ????? ????? ????? ??? ???.
?? (?????? ??????????): ?? ??? ????? ??????? ??? ??????
??? ??????: ???? ????? ??????? ??? ??? ?????? ???? ????? (??? ???? ??? ?????? 0).
?? ???? ?????: ???? ????? ??????? ??? ??? ?????? ??? ??????? (??? ?????? ??? 0)? ??? ?? ??? ?????? ?????? ????? ?? ?????? ?????.
??? ?????: ????? ??????? ???? ??? ??? ?????? ??????? ?? ????? ????? ??????.
on-abort: ????? ?????? ??? ??? ???? ????? ????? ??? ???????.
??? ???????: ????? ??????? ??? ??? ?????? ??????.
??????: ??? ????? ?? ??? ??????? ?????? ?? ??? ????? ???????.
??????? ?????? ???????? ????? ???????? ???
on-failure
??????? ??????? ???? ???? ????? ????? ?? ??????? ???? ??????? ???
on-abnormal
。
??????? ??
RestartSec
???.
RestartSec
?????: ???? ??? ??? ??????? ???? ??? ???????? ??? ????? ????? ?????? ?????? Systemd. ?????? ????? ?? ?????? ???????? ???? 42 ?????.
Install
??????? ????? ????? ????? ??? ????? ????????? ?? ????? ???? ???? ??? ????? ??????.
WantedBy
?????: ????? ??? ????? ???? ?????? ??? ??? ??????.
Target
???? ?????? ??????? ????? ??? ?????? ?? ???????.
WantedBy=multi-user.target
???? ??? ?? ????? ???? ???? ??? sshd ??
multi-user.target
??? ??????? ??? ???? ???? ????
systemctl
enable sshd.service
??? ????? ????????
sshd.service
???? ????? ???? ???? ??
/etc/systemd/system
??? ??????
multi-user.target.wants
?? ?????? ??????. ????? Systemd ??? ??? ??? ???????.
xxxxxxxxxx
$ systemctl get-default
multi-user.target
???? ??????? ????? ?? ????? ????????? ??????? ??
multi-user.target
????? ???? ??????? ?? ??? ???????? ??? ????? ??????. ??? ?? ?????.
systemctl
enable
????? ???? ?? ???? ????? ??? ???????. ??? ??????? Target?
systemctl
list-dependencies
??????? ?
systemctl isolate
??????? ????? ?????.
xxxxxxxxxx
# ??? ???? ??????? ???? ??????? multi-user.target
$ systemctl list-dependencies multi-user.target
?
# ??????? ??? ??? ???
# shutdown.target ?? ???? ???????
$ sudo systemctl isolate shutdown.target
???? ???? ???? ????? ?????? ?? ???????: ???? ??
multi-user.target
? ???? ??? ???? ??? ??????? ???????? ??????????? ????? ??
graphical.target
? ????? ???? ????? ???????? ????????? ??? ????? ???
multi-user.target
???? ?? ??????? ??????? ???? ????? ????
??? ????? ??????? ???????? ?????
。
????? Target ????? ??? ??? ??????? ????? ???.
xxxxxxxxxx
$ systemctl cat multi-user.target
?
[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes
???? ?????? ?? ??? ????? Target ?? ????? ??? ??? ??? ???????. ??????? ???????? ?????? ?? ??????? ???????? ????? ?? ??? ???.
Requires
?????: ?????????basic.target
????? ????.
Conflicts
???: ??? ??????. ???rescue.service
??rescue.target
??? ????????multi-user.target
?? ???? ??????? ?????? ????.
After
: ??? ???multi-user.target
??basic.target
、rescue.service
、rescue.target
??? ???? ??? ???? ????? ??????? ???????.
AllowIsolate
?????? ??????????systemctl isolate
????? ??????? ???multi-user.target
。
??? ????? ??? ???????? ??? ????? ????? ??? ???????? ?? ????? ????? ??????? ??? ?????.
x
# ????? ????? ??? ???????
$ sudo systemctl daemon-reload
?
# ????? ????? ??????? ??? ?????
$ sudo systemctl restart foobar
(???????)
????? ???? ?????: ????? ??? ??? - ??? ?????? - ??? ????? - ?????? ??? ?????? ???? ???????? ????????? 3.0 )
????? ?????: 8 ???? 2016
??? ??????? ??????: