systemctl 指令

之前使用 CentOS 時,安裝伺服器套件後,大部分都是使用 chkconfig 來啟動預設要啟動的服務,或是直接到 /etc/init.d 目錄之下,去啟動要使用的服務,可是安裝 CentOS 7 之後,發現 /etc/init.d 目錄下的檔案非常的少,似乎新版本的 CentOS 服務,都不是使用這樣的啟動方式。
# ls /etc/init.d
functions  iprdump  iprinit  iprupdate  netconsole  network  README

後來在網路搜尋了一下,才發現好像是改用 systemctl 指令來做控制。[@more@]systemctl 指令參數
# systemctl –help
systemctl [OPTIONS…] {COMMAND} …

Query or send control commands to the systemd manager.

  -h –help           Show this help
     –version        Show package version
  -t –type=TYPE      List only units of a particular type
     –state=STATE    List only 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.
     –reverse        Show reverse dependencies with ‘list-dependencies’
  -l –full           Don’t ellipsize unit names on output
     –fail           When queueing a new job, fail if conflicting jobs are
                      pending
     –irreversible   When queueing a new job, make sure it cannot be implicitly
                      cancelled
     –ignore-dependencies
                      When queueing a new job, ignore all its dependencies
     –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
  -H –host=[USER@]HOST
                      Show information for remote host
  -P –privileged     Acquire privileges before execution
  -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      When enabling/disabling unit files, don’t reload daemon
                      configuration
     –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
     –system         Connect to system manager
     –user           Connect to user service manager
     –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
     –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-monotonic,
                      verbose, export, json, json-pretty, json-sse, cat)
     –plain          Print unit dependencies as a list instead of a tree

Unit Commands:
  list-units                      List loaded units
  list-sockets                    List loaded sockets ordered by address
  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,

啟動 vsftpd FTP Server
# systemctl start vsftpd

顯示 vsftpd FTP Server 狀態
# systemctl status vsftpd
vsftpd.service – Vsftpd ftp daemon
   Loaded: loaded (/usr/lib/systemd/system/vsftpd.service; disabled)
   Active: active (running) since 四 2014-07-10 20:34:41 CST; 41s ago
  Process: 34160 ExecStart=/usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf (code=exited, status=0/SUCCESS)
 Main PID: 34161 (vsftpd)
   CGroup: /system.slice/vsftpd.service
           └─34161 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf

 7月 10 20:34:41 localhost.localdomain systemd[1]: Starting Vsftpd ftp daemon…
 7月 10 20:34:41 localhost.localdomain systemd[1]: Started Vsftpd ftp daemon.

關閉 vsftpd FTP Server
# systemctl stop vsftpd

列出 vsftpd FTP Server 啟動狀態
# systemctl list-unit-files | grep vsftpd
vsftpd.service                              disabled
vsftpd@.service                             disabled
vsftpd.target                               disabled

開機時啟動 vsftpd FTP Serve
# systemctl enable vsftpd
ln -s ‘/usr/lib/systemd/system/vsftpd.service’ ‘/etc/systemd/system/multi-user.target.wants/vsftpd.service

檢查是否有設定成功
# systemctl list-unit-files | grep vsftpd
vsftpd.service                              enabled
vsftpd@.service                             disabled
vsftpd.target                               disabled

# systemctl is-active vsftpd
inactive

開機時關閉 vsftpd FTP Serve
# systemctl disable vsftpd
rm ‘/etc/systemd/system/multi-user.target.wants/vsftpd.service’

檢查是否有設定成功
# systemctl list-unit-files | grep vsftpd
vsftpd.service                              disabled
vsftpd@.service                             disabled
vsftpd.target                               disabled

顯示目前系統預設運作層級
# systemctl get-default
multi-user.target

切換運作層級至 GUI 模式(runlevel 5)
# systemctl isolate graphical.target

切換運作層級至文字模式(runlevel 3)
# systemctl isolate multi-user.target

設定開機啟動至文字模式(runlevel 3)
# systemctl set-default multi-user.target

設定開機啟動至 GUI 模式(runlevel 5)
# systemctl set-default graphical.target

列出可用的層級
# systemctl list-units –type target
UNIT                  LOAD   ACTIVE SUB    DESCRIPTION
basic.target          loaded active active Basic System
cryptsetup.target     loaded active active Encrypted Volumes
getty.target          loaded active active Login Prompts
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-lookup.target     loaded active active Host and Network Name Lookups
paths.target          loaded active active Paths
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
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 of SUB.
SUB    = The low-level unit activation state, values depend on unit type.

17 loaded units listed. Pass –all to see loaded but inactive units, too.
To show all installed unit files use ‘systemctl list-unit-files’.