Systemd 是一系列工具的集合,其作用也遠(yuǎn)遠(yuǎn)不僅是啟動(dòng)操作系統(tǒng),它還接管了后臺(tái)服務(wù)、結(jié)束、狀態(tài)查詢,以及日志歸檔、設(shè)備管理、電源管理、定時(shí)任務(wù)等許多職責(zé),并支持通過特定事件(如插入特定 USB 設(shè)備)和特定端口數(shù)據(jù)觸發(fā)的 On-demand(按需)任務(wù)。
Unit 文件按照 Systemd 約定,應(yīng)該被放置指定的三個(gè)系統(tǒng)目錄之一中。這三個(gè)目錄是有優(yōu)先級(jí)的,如下所示,越靠上的優(yōu)先級(jí)越高。因此,在三個(gè)目錄中有同名文件的時(shí)候,只有優(yōu)先級(jí)最高的目錄里的那個(gè)文件會(huì)被使用。
# systemctl list-units --type=target
UNITLOADACTIVESUBDESCRIPTION
basic.target loaded active active Basic System
cryptsetup.target loaded active active Encrypted Volumes
getty.target loaded active active Login Prompts
graphical.target loaded active active Graphical Interface
local-fs-pre.target loaded active active Local File Systems(Pre)
local-fs.target loaded active active Local File Systems
multi-user.target loaded active active Multi-User System
network-online.target loaded active active Network is Online
network.target loaded active active Network
nss-user-lookup.target loaded active active User and Group Name Lookups
paths.target loaded active active Paths
remote-fs-pre.target loaded active active Remote File Systems(Pre)
remote-fs.target loaded active active Remote File Systems
slices.target loaded active active Slices
sockets.target loaded active active Sockets
sound.target loaded active active Sound Card
swap.target loaded active active Swap
sysinit.target loaded active active System Initialization
time-sync.target loaded active active System Time Synchronized
timers.target loaded active active Timers
LOAD= Reflects whether the unit definition was properly loaded.ACTIVE= The high-level unit activation state, i.e. generalization ofSUB.SUB= The low-level unit activation state, values depend on unit type.20 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
Service 段
用來 Service 的配置,只有 Service 類型的 Unit 才有這個(gè)區(qū)塊。它的主要字段分為服務(wù)生命周期和服務(wù)上下文配置兩個(gè)方面。
在 Unit 文件中,有時(shí)會(huì)需要使用到一些與運(yùn)行環(huán)境有關(guān)的信息,例如節(jié)點(diǎn) ID、運(yùn)行服務(wù)的用戶等。這些信息可以使用占位符來表示,然后在實(shí)際運(yùn)行被動(dòng)態(tài)地替換實(shí)際的值。
# systemctl --help
systemctl [OPTIONS...]{COMMAND}...
Query or send control commands to the systemd manager.-h --help Show this help
--version Show package version
--system Connect to system manager
-H--host=[USER@]HOST
Operate on remote host
-M--machine=CONTAINER
Operate on local container
-t --type=TYPE List units of a particular type
--state=STATE List units with particular LOAD or SUB or ACTIVE state
-p --property=NAME Show only properties by this name
-a --all Show all loaded units/properties, including dead/empty
ones. To list all units installed on the system, use
the 'list-unit-files' command instead.-l --full Don't ellipsize unit names on output
-r --recursive Show unit list of host and local containers
--reverse Show reverse dependencies with'list-dependencies'--job-mode=MODE Specify how to deal with already queued jobs, when
queueing a newjob--show-types When showing sockets, explicitly show their type
-i --ignore-inhibitors
When shutting down or sleeping, ignore inhibitors
--kill-who=WHO Who to send signal to
-s --signal=SIGNAL Which signal to send
--now Start or stop unit in addition to enabling or disabling it
-q --quiet Suppress output
--no-block Do not wait until operation finished
--no-wall Don't send wall message before halt/power-off/reboot
--no-reload Don't reload daemon after en-/dis-abling unit files
--no-legend Do not print a legend(column headers and hints)--no-pager Do not pipe output into a pager
--no-ask-password
Do not ask for system passwords
--global Enable/disable unit files globally
--runtime Enable unit files only temporarily until next reboot
-f --force When enabling unit files, override existing symlinks
When shutting down, execute action immediately
--preset-mode= Apply only enable, only disable, or all presets
--root=PATH Enable unit files in the specified root directory
-n --lines=INTEGER Number of journal entries to show
-o --output=STRING Change journal output mode(short, short-iso,
short-precise, short-monotonic, verbose,export, json, json-pretty, json-sse, cat)--plain Print unit dependencies as a list instead of a tree
Unit Commands:
list-units [PATTERN...] List loaded units
list-sockets [PATTERN...] List loaded sockets ordered by address
list-timers [PATTERN...] List loaded timers ordered by next elapse
start NAME...Start(activate) one or more units
stop NAME...Stop(deactivate) one or more units
reload NAME... Reload one or more units
restart NAME... Start or restart one or more units
try-restart NAME... Restart one or more units if active
reload-or-restart NAME... Reload one or more units if possible,
otherwise start or restart
reload-or-try-restart NAME... Reload one or more units if possible,
otherwise restart if active
isolate NAME Start one unit and stop all others
kill NAME... Send signal to processes of a unit
is-active PATTERN... Check whether units are active
is-failed PATTERN... Check whether units are failed
status [PATTERN...|PID...] Show runtime status of one or more units
show [PATTERN...|JOB...] Show properties of one or more
units/jobs or the manager
cat PATTERN... Show files and drop-ins of one or more units
set-property NAMEASSIGNMENT... Sets one or more properties of a unit
help PATTERN...|PID... Show manual for one or more units
reset-failed [PATTERN...] Reset failed state for all, one, or more
units
list-dependencies [NAME] Recursively show units which are required
or wanted by this unit or by which this
unit is required or wanted
Unit File Commands:
list-unit-files [PATTERN...] List installed unit files
enable NAME... Enable one or more unit files
disable NAME... Disable one or more unit files
reenable NAME... Reenable one or more unit files
preset NAME... Enable/disable one or more unit files
based on preset configuration
preset-all Enable/disable all unit files based on
preset configuration
is-enabled NAME... Check whether unit files are enabled
mask NAME... Mask one or more units
unmask NAME... Unmask one or more units
link PATH... Link one or more units files into
the search path
add-wants TARGETNAME... Add 'Wants' dependency for the target
on specified one or more units
add-requires TARGETNAME... Add 'Requires' dependency for the target
on specified one or more units
edit NAME... Edit one or more unit files
get-default Get the name of the default target
set-defaultNAME Set the default target
Machine Commands:
list-machines [PATTERN...] List local containers and host
Job Commands:
list-jobs [PATTERN...] List jobs
cancel [JOB...] Cancel all, one, or more jobs
Snapshot Commands:
snapshot [NAME] Create a snapshot
deleteNAME... Remove one or more snapshots
Environment Commands:
show-environment Dump environment
set-environment NAME=VALUE... Set one or more environment variables
unset-environment NAME... Unset one or more environment variables
import-environment [NAME...] Import all or some environment variables
Manager Lifecycle Commands:
daemon-reload Reload systemd manager configuration
daemon-reexec Reexecute systemd manager
System Commands:
is-system-running Check whether system is fully running
default Enter system default mode
rescue Enter system rescue mode
emergency Enter system emergency mode
halt Shut down and halt the system
poweroff Shut down and power-off the system
reboot [ARG] Shut down and reboot the system
kexec Shut down and reboot the system with kexec
exit Request user instance exit
switch-root ROOT[INIT] Change to a different root file system
suspend Suspend the system
hibernate Hibernate the system
hybrid-sleep Hibernate and suspend the system
Unit 管理
查看當(dāng)前系統(tǒng)的所有 Unit
# 列出正在運(yùn)行的 Unit
$ systemctl list-units
# 列出所有Unit,包括沒有找到配置文件的或者啟動(dòng)失敗的
$ systemctl list-units --all
# 列出所有沒有運(yùn)行的 Unit
$ systemctl list-units --all --state=inactive
# 列出所有加載失敗的 Unit
$ systemctl list-units --failed
# 列出所有正在運(yùn)行的、類型為 service 的 Unit
$ systemctl list-units --type=service
# 查看 Unit 配置文件的內(nèi)容
$ systemctl cat docker.service
# 顯示系統(tǒng)狀態(tài)
$ systemctl status
# 顯示單個(gè) Unit 的狀態(tài)
$ ystemctl status bluetooth.service
# 顯示遠(yuǎn)程主機(jī)的某個(gè) Unit 的狀態(tài)
$ systemctl -H root@rhel7.example.com status httpd.service