每天学五分钟 Liunx 0110 | 服务篇:守护进程 systemd
有些进程会在系统上运行较长时间,如前面的 Hello World 程序运行时产生的进程。有些进程运行瞬间就结束了,如执行 ps 命令产生的进程,也有的进程会常驻在内存中,提供相应的服务,这样的进程称为守护进程 (daemon),它所提供的功能叫做服务。
systemd
systemd 命令
系统管理
|
systemctl
|
重启系统
sudo systemctl reboot
暂停系统
sudo systemctl suspend
CPU 停止工作
sudo systemctl halt
关闭系统,切断电源
sudo systemctl poweroff
|
systemd-analyze
|
查看启动耗时
systemd-analyze
查看每个服务启动耗时
systemd-analyze blame
查看启动过程流
systemd-analyze critical-chain
查看指定服务的启动流
systemd-analyze critical-chain sshd.service
|
|
hostnamectl
|
查看当前主机信息
hostnamectl
设置主机名
sudo hostnamectl set-hostname ***
|
|
localectl
|
查看本地化设置
localectl
|
|
timedatectl
|
查看当前时区设置
timedatectl
显示所有可用时区
timedatectl list-timezones
设置当前时区
sudo timedatectl set-timezone Africa/Juba
|
|
Unit
|
systemctl list-units
|
查看系统正在运行的单元
systemctl list-units
列出系统中所有单元
systemctl list-units --all
列出所有加载失败的单元
systemctl list-units --failed
列出所有正在运行,类型为服务的单元
systemctl list-units --type=service
|
systemctl status
|
查看系统状态
systemctl status
查看服务状态
systemctl status ***.service
查看远端主机的服务状态
systemctl -H user@hostname status ***.service
|
|
unit 管理
|
启动服务
sudo systemctl start ***.service
停止服务
sudo systemctl stop ***.service
重启服务
sudo systemctl restart ***.service
杀死服务产生的所有子进程
sudo systemctl kill ***.service
重新加载服务配置文件
sudo systemctl reload ***.service
重新加载所有修改单元的配置文件
sudo systemctl daemon-reload
显示 unit 的底层参数
sudo systemctl show ***.service
|
|
systemctl list-dependencies
|
列出服务的依赖关系
systemctl list-dependencies ***.service
列出服务的反向依赖关系
systemctl list-dependencies --reverse ***.service
查看 target 包含的 unit
systemctl list-dependencies ***.target
|
|
unit 配置文件
|
查看 unit 配置文件的内容
systemctl cat ***.service
查看所有配置文件
systemctl list-unit-files
查看指定类型的配置文件
systemctl list-unit-files --type=service
|
|
Target
|
查看系统的所有 target
systemctl list-unit-files --type=target
查看 target 包含的 unit
systemctl list-dependencies ***.target
查看启动时的默认 target
systemctl get-default
|
|
日志管理
|
journalctl
|
查看本次系统启动的所有日志
journalctl
查看指定 Unit 的日志
journalctl -u ***.service
滚动显示最新的日志
journalctl -f
查看指定时间的日志
journalctl --since "2020-05-06 01:00" --until "2020-05-06 03:00"
|
[root@lianhua ~]$ systemd-analyze critical-chain sshd.service 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.sshd.service +16ms
└─cloud-init.service @6.503s +9.732s
└─network.service @3.352s +3.121s
└─network-pre.target @3.348s
└─cloud-init-local.service @1.665s +1.682s
└─dbus.socket @1.659s
└─sysinit.target @1.628s
└─systemd-update-utmp.service @1.614s +13ms
└─auditd.service @1.424s +182ms
└─systemd-tmpfiles-setup.service @1.394s +16ms
└─rhel-import-state.service @1.341s +40ms
└─local-fs.target @1.325s
└─ephemeral.mount @747ms +577ms
└─local-fs-pre.target @728ms
└─lvm2-monitor.service @368ms +359ms
└─lvm2-lvmetad.service @484ms
└─lvm2-lvmetad.socket @329ms
└─-.slice
Unit
-
Service unit:系统服务
-
Target unit:多个 Unit 构成的一个组
-
Device Unit:硬件设备
-
Mount Unit:文件系统的挂载点
-
Automount Unit:自动挂载点
-
Path Unit:文件或路径
-
Scope Unit:不是由 Systemd 启动的外部进程
-
Slice Unit:进程组
-
Snapshot Unit:Systemd 快照,可以切回某个快照
-
Socket Unit:进程间通信的 socket
-
Swap Unit:swap 文件
-
Timer Unit:定时器
[root@lianhua ~]$ systemctl status sshd.service ● sshd.service - OpenSSH server daemon Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)Active: active (running) since Fri 2019-11-01 18:35:20 CST; 6 months 3 days ago Docs: man:sshd(8) man:sshd_config(5) Main PID: 8621 (sshd) Memory: 24.7M CGroup: /system.slice/sshd.service └─8621 /usr/sbin/sshd -DNov 01 18:35:20 lianhua systemd[1]: Starting OpenSSH server daemon...
[root@lianhua ~]$ systemctl list-dependencies sshd.service sshd.service ● ├─sshd-keygen.service ● ├─system.slice ● └─basic.target ● ├─microcode.service ● ├─rhel-dmesg.service ● ├─selinux-policy-migrate-local-changes@targeted.service ...
[root@lianhua ~]$ systemctl list-dependencies --reverse sshd.service sshd.service ● ├─cloud-init.service ● └─multi-user.target ● └─graphical.target
[root@lianhua ~]$ systemctl cat sshd.service # /usr/lib/systemd/system/sshd.service [Unit] Description=OpenSSH server daemon Documentation=man:sshd(8) man:sshd_config(5) After=network.target sshd-keygen.service Wants=sshd-keygen.service[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s[Install]
WantedBy=multi-user.target
-
simple:默认值,使用主进程执行 ExecStart 指定的命令。
-
forking:以 fork 的方式从父进程中创建子进程,创建后父进程立即退出。
-
oneshot:一次性进程,systemd 等待当前服务退出,再往下执行。
-
dbus:当前服务通过 D-Bus 启动。
-
notify:当前服务启动完毕,通知 systemd ,再继续往下执行。
-
idle: 若有其它服务执行完毕,当前服务才会运行。
Unit 配置文件状态
[root@lianhua ~]$ systemctl list-unit-files UNIT FILE STATE tmp.mount disabled brandbot.path enabled auth-rpcgss-module.service static autofs.service disabled systemd-timedated.service masked ...
# autofs.service disabled [root@lianhua ~]# systemctl cat autofs.service # /usr/lib/systemd/system/autofs.service [Unit] ... [Service] ... [Install] WantedBy=multi-user.target [root@lianhua ~]# ll /etc/systemd/system/multi-user.target.wants/ | grep autofs.service [root@lianhua ~]#brandbot.path enabled
[root@lianhua ~]$ systemctl cat brandbot.path
/usr/lib/systemd/system/brandbot.path
[Unit]
...
[Path]
...
[Install]
WantedBy=multi-user.target
[root@lianhua ~]$ ll /etc/systemd/system/multi-user.target.wants/ | grep brand
lrwxrwxrwx. 1 root root 37 May 9 2018 brandbot.path -> /usr/lib/systemd/system/brandbot.pathsystemd-timedated.service masked
[root@lianhua ~]# ll /etc/systemd/system | grep systemd-timedated.service
lrwxrwxrwx. 1 root root 9 Aug 12 2018 systemd-timedated.service -> /dev/nullauth-rpcgss-module.service static
[root@lianhua ~]# systemctl cat auth-rpcgss-module.service
[Unit]
...
[Service]
...
[root@lianhua ~]# systemctl is-enabled autofs.service disabled [root@lianhua ~]# systemctl enable autofs.service Created symlink /etc/systemd/system/multi-user.target.wants/autofs.service → /usr/lib/systemd/system/autofs.service. [root@lianhua ~]# systemctl is-enabled autofs.service enabled
[root@lianhua ~]# systemctl status autofs.service ● autofs.service - Automounts filesystems on demand Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled; vendor preset: disabled)Active: inactive (dead)
/etc/systemd/system-preset/*.preset /run/systemd/system-preset/*.preset /usr/lib/systemd/system-preset/*.preset /etc/systemd/user-preset/*.preset /run/systemd/user-preset/*.preset /usr/lib/systemd/user-preset/*.preset
[root@lianhua ~]# ll /usr/lib/systemd/system-preset/ total 24 -rw-r--r--. 1 root root 264 Sep 25 2019 85-display-manager.preset -rw-r--r--. 1 root root 3982 Sep 25 2019 90-default.preset -rw-r--r--. 1 root root 951 Jun 22 2018 90-systemd.preset[root@lianhua ~]# cat /usr/lib/systemd/system-preset/90-systemd.preset
enable remote-fs.target
disable console-getty.service
disable debug-shell.service
[root@lianhua process]$ systemctl status autofs.service ● autofs.service - Automounts filesystems on demand Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled; vendor preset: disabled)Active: active (running) since Mon 2019-08-05 17:42:46 CST; 9 months 0 days ago Process: 23921 ExecReload=/usr/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)[root@lianhua process]$ cat /usr/lib/systemd/system/autofs.service.d/app.conf
[Service]
User=
User=app
[root@lianhua process]$ systemctl daemon-reload
[root@lianhua process]$ systemctl restart autofs.service
[root@lianhua process]$ systemctl status autofs.service
● autofs.service - Automounts filesystems on demand
Loaded: loaded (/usr/lib/systemd/system/autofs.service; enabled; vendor preset: disabled)
Drop-In: /usr/lib/systemd/system/autofs.service.d
└─app.conf
Active: active (running) since Mon 2019-08-05 17:42:46 CST; 9 months 0 days ago
Target
[root@lianhua ~]# systemctl cat cloud-config.target [Unit] Description=Cloud-config availability Wants=cloud-init-local.service cloud-init.service After=cloud-init-local.service cloud-init.service [root@lianhua ~]# systemctl status cloud-config.target ● cloud-config.target - Cloud-config availability Loaded: loaded (/usr/lib/systemd/system/cloud-config.target; static; vendor preset: disabled) Active: active since Fri 2020-03-13 09:18:36 UTC; 1 months 22 days ago
path unit
[root@lianhua ~]# systemctl status systemd-ask-password-wall.path ● systemd-ask-password-wall.path - Forward Password Requests to Wall Directory Watch Loaded: loaded (/usr/lib/systemd/system/systemd-ask-password-wall.path; static; vendor preset: disabled)Active: active (waiting) since Fri 2020-03-13 09:17:52 UTC; 1 months 22 days ago Docs: man:systemd-ask-password-console.service(8)[root@lianhua ~]# systemctl cat systemd-ask-password-wall.path
[Unit]
Description=Forward Password Requests to Wall Directory Watch
...[Path]
DirectoryNotEmpty=/run/systemd/ask-password
MakeDirectory=yes
[root@lianhua ~]# systemctl status systemd-ask-password-wall
● systemd-ask-password-wall.service - Forward Password Requests to Wall
Loaded: loaded (/usr/lib/systemd/system/systemd-ask-password-wall.service; static; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd-ask-password-console.service(8)
mount unit
[root@lianhua process]$ systemctl status build.mount ● build.mount - /build Loaded: loaded (/etc/fstab; bad; vendor preset: disabled)Active: active (mounted) since Mon 2019-08-05 17:42:44 CST; 9 months 0 days ago Where: /build What: lianhua.net:/vol/test/build/build Docs: man:fstab(5) man:systemd-fstab-generator(8) Memory: 0BAug 05 17:42:44 lianhua systemd[1]: Mounting /build...
Aug 05 17:42:44 lianhua systemd[1]: Mounted /build.
[root@lianhua process]$ systemctl cat build.mount[Unit]
SourcePath=/etc/fstab
Documentation=man:fstab(5) man:systemd-fstab-generator(8)
Before=remote-fs.target[Mount]
What=lianhua.net:/vol/test/build/build
Where=/build
Type=nfs
Options=soft,intr,retry=1,rw,rsize=32768,wsize=32768
[root@lianhua process]$ df -hT | grep /build
lianhua.net:/vol/test/build/build nfs 21T 17T 4.3T 80% /build