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