systemctl詳解


[root@hadoop01 hadoop]# 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 new job
     --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 NAME ASSIGNMENT... 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

systemctl [OPTIONS...] {COMMAND} ...

Query or send control commands to the systemd manager.

  -h --help           Show this help
     --version        Show package version
systemctl [OPTIONS...] {COMMAND} ...

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 new job
     --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 NAME ASSIGNMENT... 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 TARGET NAME...        Add 'Wants' dependency for the target
                                  on specified one or more units
  add-requires TARGET NAME...     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-default NAME                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
  delete NAME...                  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

一、Systemd簡介

Systemd是由紅帽公司的一名叫做Lennart Poettering的員工開發,systemd是Linux系統中最新的初始化系統(init),它主要的設計目的是克服Sys V 固有的缺點,提高系統的啟動速度。

systemd和upstart是競爭對手,ubantu上使用的是upstart的啟動方式,centos7上使用systemd替換了Sys V,Systemd目錄是要取代Unix時代依賴一直在使用的init系統,兼容SysV和LSB的啟動腳本,

而且能夠在進程啟動中更有效地引導加載服務。
system:系統啟動和服務器守護進程管理器,負責在系統啟動或運行時,激活系統資源,服務器進程和其他進程,根據管理,字母d是守護進程(daemon)的縮寫,systemd這個名字的含義就是它要守護整個系統。

 

二、Systemd新特性

  • 系統引導時實現服務並行啟動

  • 按需啟動守護進程

  • 自動化的服務依賴關系管理

  • 同時采用socket式與D-Bus總線式激活服務

  • 系統狀態快照和恢復

  • 利用Linux的cgroups監視進程

  • 維護掛載點和自動掛載點

  • 各服務間基於依賴關系進行精密控制

 

三、Systemd核心概念

  • Unit
    表示不同類型的sytemd對象,通過配置文件進行標識和配置,文件中主要包含了系統服務,監聽socket、保存的系統快照以及其他與init相關的信息

  • 配置文件:
    /usr/lib/systemd/system:每個服務最主要的啟動腳本設置,類似於之前的/etc/initd.d
    /run/system/system:系統執行過程中所產生的服務腳本,比上面的目錄優先運行
    /etc/system/system:管理員建立的執行腳本,類似於/etc/rc.d/rcN.d/Sxx類的功能,比上面目錄優先運行,在三者之中,此目錄優先級最高

 

四、Unit類型

  • systemctl -t help :查看unit類型

  • service unit:文件擴展名為.service,用於定義系統服務

  • target unit:文件擴展名為.target,用於模擬實現“運行級別”

  • device unit: .device,用於定義內核識別的設備

  • mount unit :.mount,定義文件系統掛載點

  • socket unit :.socket,用於標識進程間通信用的socket文件,也可以在系統啟動時,延遲啟動服務,實現按需啟動

  • snapshot unit:.snapshot,關系系統快照

  • swap unit:.swap,用於表示swap設備

  • automount unit:.automount,文件系統的自動掛載點如:/misc目錄

  • path unit:.path,用於定義文件系統中的一個文件或目錄使用,常用於當文件系統變化時,延遲激活服務,如spool目錄

  • time:.timer由systemd管理的計時器

    注:使用systemctl控制單元時,通常需要使用單元文件的全名,包括擴展名,但是有些單元可以在systemctl中使用簡寫方式,如果無擴展名,systemctl默認把擴展名當做.service。例如netcfg和netcfg.service是等同的掛載點會自動轉化為相應的.mount單元,例如/home等價於home.mount設備會自動轉化為相應的.device單元,所以/dev/sd2等價於dev-sda2.device

 

五、關鍵特性

  • 基於socket的激活機制:socket與服務進程分離

  • 基於D-Bus的激活機制

  • 基於device的激活機制

  • 基於path的激活機制

  • 系統快照:保存各unit的當前狀態信息於持久存儲設備中想后兼容sysv init腳本

 

六、不兼容

  • systemctl命令固定不變,不可擴展

  • 非由systemd啟動的服務,systemctl無語與之通信和控制,如:使用之前sys v風格管理的進程就無法收systemd控制

 

七、Systemd基本工具

監視和控制systemd的主要命令是systemctl。該命令可用於查看系統狀態和管理系統及服務。

  • 管理服務

  命令:systemctl  command name.service

  啟動:service name start –>systemctl start name.service

  停止:service name stop –>systemctl stop name.service

  重啟:service name restart–>systemctl restart name.service

  狀態:service name status–>systemctl status name.service

  • 條件式重啟(已啟動才重啟,否則不做任何操作)
    systemctl try-restart name.service

  • 重載或重啟服務(先加載,然后再啟動)
    systemctl reload-or-try-restart name.service

  • 禁止自動和手動啟動
    systemctl mask name.service  
    執行此條命令實則創建了一個鏈接 ln -s '/dev/null' '/etc/systemd/system/sshd.service'

  • 取消禁止
    systemctl unmask name.service  
    刪除此前創建的鏈接

  • 服務查看(查看某服務當前激活與否的狀態)
    systemctl is-active name.service  
    如果啟動會顯示active,否則會顯示unknown

  • 查看所有已經激活的服務
    systemctl list-units –t|–type service

[root@hadoop01 hadoop]# systemctl  --helpsystemctl [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 new job     --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 NAME ASSIGNMENT... 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
systemctl [OPTIONS...] {COMMAND} ...
Query or send control commands to the systemd manager.
  -h --help           Show this help     --version        Show package versionsystemctl [OPTIONS...] {COMMAND} ...
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 new job     --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 NAME ASSIGNMENT... 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 TARGET NAME...        Add 'Wants' dependency for the target                                  on specified one or more units  add-requires TARGET NAME...     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-default NAME                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  delete NAME...                  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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM