Linux命令:systemctl命令詳解

最近更新時間 2020-01-01 17:18:19

systemctl主要用於控制Systemd系統和管理系統中運行的服務。

Systemd是為了解決傳統Linux系統中System V風格init進程管理的缺點而誕生,傳統init進程啟動時間長,只有前一個進程啟動完才會啟動下一個進程,啟動腳本複雜,服務需要編寫自己管理腳本。Systemd採用以下新技術:

  • 將service、target、mount、timer、socket、swap等稱為Unit。
  • 採用Socket激活式與D-Bus激活式服務,以提高相互依賴的各服務的並行運行性能。
  • 用cgroups代替進程ID來追蹤進程,以此即使是兩次fork之後生成的守護進程也不會脱離systemd的控制。
  • 用target代替System V的運行級別(Runlevel),比如,SystemD的graphical.target相當於System V的init 5,multi-user.target相當於System V的init 3。
  • 內置新的journald 日誌管理系統。
  • 引入localectl、timedatectl等新命令,系統配置更方便。

Systemd開發目標是最終代替現在常用的System V與BSD風格init程序。

Systemd已納入眾多Linux發行版的軟件源中,比如Red Hat Enterprise Linux 7及後續版本,包括其派生品CentOS、Scientific Linux、Oracle Linux等。

語法格式

systemctl [OPTIONS...] COMMAND [NAME...]

常用命令

基礎命令

1. 查看系統是否已經安裝systemctl。

systemd --version
systemd 219
+PAM +AUDIT +SELINUX +IMA -APPARMOR ...
whereis systemctl
systemctl: /usr/bin/systemctl /usr/share/man/man1/systemctl.1.gz

2.  查看服務啟動時間。

systemd-analyze blame
5.963s dev-mapper-centos\x2droot.device
4.561s lvm2-monitor.service
3.007s NetworkManager-wait-online.service
1.900s network.service
1.857s tuned.service
1.346s firewalld.service
1.274s postfix.service
1.207s boot.mount
 560ms polkit.service
 512ms lvm2-pvscan@8:2.service

3. 查看嚴重消耗時間的服務樹狀表。

systemd-analyze critical-chain
The time after the unit is active or started is printed after the "@" character.
The time the unit takes to start is printed after the "+" character.

multi-user.target @15.357s
└─tuned.service @13.500s +1.857s
  └─network.target @13.498s
    └─NetworkManager.service @9.800s +168ms
      └─network-pre.target @9.791s
        └─firewalld.service @8.438s +1.346s
          └─polkit.service @7.867s +560ms
            └─basic.target @7.855s
              └─sockets.target @7.855s
                └─dbus.socket @7.854s
                  └─sysinit.target @7.849s
                    └─systemd-update-utmp.service @7.821s +27ms
                      └─auditd.service @7.506s +307ms
                        └─systemd-tmpfiles-setup.service @7.228s +273ms
                          └─rhel-import-state.service @6.790s +436ms
                            └─local-fs.target @6.737s

4. 顯示系統中所有可用的units。 

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
......

5. 顯示所有正在運行的units。 

systemctl list-units
 UNIT                                     LOAD   ACTIVE SUB       DESCRIPTION
  proc-sys-fs-binfmt_misc.automount        loaded active waiting   Arbitrary Executable File Formats File S
  -.mount                                  loaded active mounted   /
  boot.mount                               loaded active mounted   /boot
  dev-hugepages.mount                      loaded active mounted   Huge Pages File System
  dev-mqueue.mount                         loaded active mounted   POSIX Message Queue File System
  systemd-ask-password-wall.path           loaded active waiting   Forward Password Requests to Wall Direct
  session-1.scope                          loaded active running   Session 1 of user root
  crond.service                            loaded active running   Command Scheduler
  dbus.service                             loaded active running   D-Bus System Message Bus
  firewalld.service                        loaded active running   firewalld - dynamic firewall daemon
  [email protected]                       loaded active running   Getty on tty1
● kdump.service                            loaded failed failed    Crash recovery kernel arming
......

6. 顯示所有失敗的units。

systemctl --failed
  UNIT          LOAD   ACTIVE SUB    DESCRIPTION
● kdump.service loaded failed failed Crash recovery kernel arming

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.

1 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

7. 檢測某個Unit(cron.service)是否啟動。

systemctl is-enabled crond.service
enabled

8. 檢測某個Unit或者服務是否正在運行。

systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-12-31 18:06:27 CST; 22h ago
     Docs: man:firewalld(1)
 Main PID: 6519 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─6519 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Dec 31 18:06:25 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
Dec 31 18:06:27 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.

 使用Systemctl管理和控制服務

9. 顯示所有服務(包括運行和關閉的服務)。

systemctl list-unit-files --type=service
UNIT FILE                                     STATE   
auditd.service                                enabled 
[email protected]                               enabled 
blk-availability.service                      disabled
brandbot.service                              static  
console-getty.service                         disabled
console-shell.service                         disabled
[email protected]                      static  
cpupower.service                              disabled
crond.service                                 enabled 
......

10. 在Linux中啟動、重啟、關閉、重新加載和檢測服務(crond.service)的運行狀態。

systemctl start crond.service systemctl restart crond.service systemctl stop crond.service systemctl reload crond.service systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since Wed 2020-01-01 16:49:29 CST; 3s ago
 Main PID: 9249 (crond)
   CGroup: /system.slice/crond.service
           └─9249 /usr/sbin/crond -n

Jan 01 16:49:29 bogon systemd[1]: Started Command Scheduler.
Jan 01 16:49:29 bogon crond[9249]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 16% if used.)
Jan 01 16:49:30 bogon crond[9249]: (CRON) INFO (running with inotify support)
Jan 01 16:49:30 bogon crond[9249]: (CRON) INFO (@reboot jobs will be run at computer's startup.)

Note: systemctlstartrestartstopreload命令不會輸出任何信息,只有status會輸出如上信息。

11. 設置開機啟動或禁用服務。

systemctl is-active httpd.service systemctl enable httpd.service systemctl disable httpd.service

12. 註銷(註銷後服務不能啟動)和取消註銷服務。

systemctl mask httpd.service
Created symlink from /etc/systemd/system/httpd.service to /dev/null.
systemctl unmask httpd.service
Removed symlink /etc/systemd/system/httpd.service.

 

rss_feed