systemd

Systemd#

Tips#

# 查看配置
systemctl show docker
# 修改 Service 需要重载
systemctl daemon-reload
# 查看定义的配置
systemctl show --property=Environment docker
# 重启服务
systemctl restart docker
systemctl list-units --all
systemctl list-units --type=service
systemctl list-unit-files
# 查看 service 文件
systemctl cat docker.service
# 查看依赖
systemctl list-dependencies sshd.service
# mask 后的服务不能被启动
systemctl mask nginx.service
systemctl unmask nginx.service
# 编辑 service 文件
systemctl edit nginx.service
# 编辑完整的文件,而不是一个片段,会生成部分内容
systemctl edit --full nginx.service
# 系统状态
systemctl status
# 服务状态
systemctl status prometheus-node-exporter.service
# 远程服务状态
systemctl -H [email protected] status httpd.service
# is-active, is-failed, is-enabled
systemctl is-active prometheus-node-exporter.service
# 管理
# start stop restart kill reload show
systemctl start apache.service
# 查看属性
systemctl show -P CPUShare apache.service
# 设置属性
systemctl set-property httpd.service CPUShares=500
#
systemctl daemon-reload
# 查看依赖
# --all - 所有
systemctl list-dependencies nginx.service
# enabled disabled static masked
# static - 无 [Install], 只能被依赖
# masked - 被禁止
systemctl list-unit-files --type=service
# 查看内容
systemctl cat atd.service
commandmean
查询-
systemctl list-dependenciesunit 依赖
systemctl list-socketssockets 状态
systemctl list-jobs活跃 jobs
systemctl list-unit-filesunit 文件和状态
systemctl list-unitsunits loaded/active
systemctl get-defaultdefault target (like run level)
服务-
systemctl stop service停止
systemctl start service启动
systemctl restart service重启
systemctl reload service配置重载
systemctl daemon-reload重载 unit 文件
systemctl status service服务状态
systemctl --failed失败的服务
systemctl reset-failed重置失败的状态
systemctl enable service服务自启动
systemctl disable service关闭自启动
systemctl show service服务信息
systemctl edit service编辑 unit
systemctl edit --full service编辑服务
systemctl -H host status network远程执行
系统状态-
systemctl reboot重启 / reboot.target
systemctl poweroff关机 / poweroff.target
systemctl emergencyemergency.target
systemctl default恢复到默认 multi-user.target
日志-
journalctl所有日志
journalctl -u network.service网络服务日志
journalctl -f跟随显示
journalctl -k内核日志
utildesc
systemctl主要命令
journalctl
notify
systemd-analyze查看启动耗时
cgls
cgtop
loginctl当前登陆用户
nspwan
hostnamectl查看和管理主机信息
localectl本地化配置
timedatectl时区
daemondesc
systemd
journald
networkd
logind
user session
unitdesc
Service系统服务
Target多个 Unit 构成的一个组
Device硬件设备
Mount文件系统的挂载点
Automount自动挂载点
Path文件或路径
Scope不是由 Systemd 启动的外部进程
Slice进程组
SnapshotSystemd 快照,可以切回某个快照
Socket进程间通信的 socket
Swapswap 文件
Timer定时器

Unit 文件#

[Unit]
Description=描述
Documentation=文档地址
Requires=强依赖 Unit
Wants=弱依赖 Unit
BindsTo=添加依赖到 Unit - 指定 Unit 退出则当前 Unit 会退出
Before=
After=
Conflicts=不能同时运行 Unit
Condition=
Assert=
[Install]
WantedBy=指定 Target 会 link 到 /etc/systemd/system/<Target>.wants
RequiredBy=指定 Target 会 link 到 /etc/systemd/system/<Target>.required
Alias=
Also=同时启动其他 Unit
[Service]
# simple - 执行 ExecStart
# forking
# oneshot - 一次性,执行完成后继续
# dbus - 通过 DBus 启动
# notify - 执行完毕通知 systemd
# idle - 有其他任务当前任务才执行
Type=simple
ExecStart=
ExecStartPre=
ExecStartPost=
ExecReload=
ExecStop=
ExecStopPost=
RestartSec=
# 重启情况 - always, on-success, on-failure, on-abnormal, on-abort, on-watchdog
Restart=
TimeoutSec=
Environment=

Target#

Unit 组, 类似于其他 init 的 runlevel 概念

runleveltarget namelink
Runlevel 0runlevel0.targetpoweroff.target
Runlevel 1runlevel1.targetrescue.target
Runlevel 2runlevel2.targetmulti-user.target
Runlevel 3runlevel3.targetmulti-user.target
Runlevel 4runlevel4.targetmulti-user.target
Runlevel 5runlevel5.targetgraphical.target
Runlevel 6runlevel6.targetreboot.target
# 查看当前系统的所有 Target
systemctl list-unit-files --type=target
# 查看一个 Target 包含的所有 Unit
systemctl list-dependencies multi-user.target
# 查看启动时的默认 Target
systemctl get-default
# 设置启动时的默认 Target
sudo systemctl set-default multi-user.target
# 切换 Target 时,默认不关闭前一个 Target 启动的进程,
# systemctl isolate 命令改变这种行为,
# 关闭前一个 Target 里面所有不属于后一个 Target 的进程
sudo systemctl isolate multi-user.target

Target vs init/RunLevel#

  • init/RunLevel
    • 默认 RunLevel /etc/inittab
    • 脚本位于 /etc/init.d - 等同于 Unit
    • 启动的脚本 link 到 /etc/rc.d 或 /etc/runlevels/
    • init 配置位于 /etc/sysconfig/etc/rc.conf
  • Target
    • 默认 /etc/systemd/system/default.target
      • link 到 multi-user.target 或 graphical.target
    • Unit 位于 /lib/systemd/system
    • 启动后位于 /etc/systemd/system
    • systemd 配置 /lib/systemd, /etc/systemd

Example#

/etc/systemd/system/prometheus-node-exporter.service

[Unit]
Description=Prometheus Node Exporter
After=network-online.target
[Service]
# User=prometheus
Restart=on-failure
ExecStart=/opt/prometheus/bin/node_exporter

FAQ#

Why archlinux migrate to systemd#