Systemctl

Systemd是Linux系统中的初始化工具, 可以有效提高系统的启动速度

Systemd默认从/etc/systemd/system/中读取配置文件, 里面的大部分文件
是符号链接, 实际指向/usr/lib/systemd/system/目录. 而Systemctl enable
实际上是在上述两个目录里边创建符号链接, 等同于

ln -s '/usr/lib/systemd/system/nginx.service' \
      '/etc/systemd/system/multi-user.target.wants/nginx.service'

将自定义程序注册为systemd service进程管理交给系统管理

参考文档
编写systemctl自启动服务

service

配置文件

更加全面文档

🐎的, 看不懂喵

[Unit]

[Unit]
## 一段描述这个Unit文件的文字
Description=nginx
## 指定服务的文档, 可以为一个或多个文档的URL
Document
## 依赖的其他Unit列表, 会在 本服务启动的同时 启动列表的服务, 
## 若有任意服务启动失败, 该服务也会终止
Requires
## 与Requires相似, 但在 本服务启动前 启动列表所有服务
After=network.target
## 与After相反, 在 本服务确保运行后 启动列表所有服务
Before
## 与Requires, 但列表服务启动失败并不终止该服务
Wants
## 与该服务有冲突的服务, 
## 若列表中有正在运行的服务, 该服务就不能启动, 反之亦然
Conflicts
## 当该服务启动失败时, 自动启动列出的每个模块
OnFailure

[Service]

[Service]
## 服务的类型, 常用有simple(默认)和forking
## forking应用于该服务启动后会通过fork系统调用创建子进程,
## 并关闭自身进程的情况
Type=forking
## 为该服务添加的环境变量
Environment
## 指定加载包含服务所需环境变量列表的文件, 文件每行都是环境变量
EnvironmentFile
## 指定服务启动的主要命令. 只使用一次, 绝对路径
ExecStart=/usr/local/nginx/sbin/nginx
## 启动执行ExecStart前的准备工作, 可有多个, 顺序执行
ExecStartPre
## 启动执行ExecStart后的收尾工作, 可有多个, 顺序执行
ExecStartPost
## 停止服务启动的主要命令, 绝对路径
ExecStop=/usr/local/nginx/sbin/nginx -s quit
## 停止服务启动等候的收尾工作, 可有多个, 顺序执行
ExecStopPost
## 重载服务的主要命令, 绝对路径
ExecReload=/usr/local/nginx/sbin/nginx -s reload 
## 指定何时重启服务, 常用值由no, on-success, on-failure, 
## on-abnormal, on-abort, always
## 默认no, 即不会自从重启服务
Restart
## 若服务器需要被重启, 参数值为服务被重启前等待秒数
RestartSec
## 服务的进程优先级, 默认为0, -20最高优先级, 19最低优先级
Nice
## 指定工作目录
WorkingDirectory
## 指定服务进程的根目录, 配置该参数后服务无法访问范围外文件
RootDirectory
## 指定运行服务用户
User
## 指定运行服务用户组
Group
## 是否给服务分配独立的临时空间
PrivateTmp=true

[Install]

[Install]
## 与Wants作用相似, 但后面列出的是依赖当前服务的服务, 
## WantedBy=multi-user.target表明系统多用户启动时, 服务自动运行
WantedBy=multi-user.target
## 与Requires作用相似, 但后面列出的是依赖当前服务的服务
RequiredBy
## 当服务被enable/disable是, 自动enable/disable后面列出的服务
Alse

启停服务

# 启动服务
systemctl start      service_name.service
# 停止服务
systemctl stop       service_name.service
# 重启服务
systemctl restart    service_name.service
# 查看服务状态
systemctl status     service_name.service
# 开机自启服务
systemctl enable     service_name.service
# 关闭开机自启服务
systemctl disable    service_name.service
# 查看服务是否开机启动
systemctl is-enabled service_name.service
# 重新读取服务配置(不用停止服务就能使修改的配置生效)
systemctl reload     service_name.service
# 重新加载systemd(一般修改完配置文件执行该命令使配置生效)
systemctl daemon-reload
# 查看已启动的服务列表
systemctl list-unit-files | grep enabled
# 查看启动失败的服务列表
systemctl --failed

分析

docker

官方源安装
# 来源154服务器
[Unit]
## 描述
Description=Docker Application Container Engine
## 帮助文档地址
Documentation=https://docs.docker.com
## 在网络 docker.socket 防火墙等服务启动后再开启该服务
After=network-online.target docker.socket firewalld.service containerd.service time-set.target
## 即使网络 containerd启动失败也不影响本服务
Wants=network-online.target containerd.service
## docker socket必须启动成功
Requires=docker.socket

[Service]
## 貌似是在启动完成后得发送一个消息才视为启动成功
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
## 运行的脚本为/usr/bin/dockered程序
## 后续的附加参数以后再查查吧(
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
## 重新加载貌似是向进程发送了重启信号(?)
ExecReload=/bin/kill -s HUP $MAINPID
## 重启等待时间???等等, 我是不是又丢笔记了?
TimeoutStartSec=0
## 重启时间?
RestartSec=2
## 总是重启?
Restart=always
## 啥东西的最大大小, 128MB
Environment="BUILDKIT_STEP_LOG_MAX_SIZE=1073741824"

# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
## 没见过, 但貌似和启动时什么限制有关
StartLimitBurst=3

# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
## 没见过, 但貌似和启动时什么限制有关
StartLimitInterval=60s

# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
## 没见过
LimitNPROC=infinity
## 没见过
LimitCORE=infinity

# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
## 没见过
TasksMax=infinity

# set delegate yes so that systemd does not reset the cgroups of docker containers
## 没见过
Delegate=yes

# kill only the docker process, not all processes in the cgroup
## 没见过
KillMode=process
## 没见过
OOMScoreAdjust=-500

[Install]
## 系统多用户启动时, 服务自动运行
WantedBy=multi-user.target
离线安装
## 某老哥的脚本
[Unit]
## 描述
Description=Docker Application Container Engine
## 文档地址
Documentation=https://docs.docker.com
## 在网络和防火墙启动后再启动服务
After=network-online.target firewalld.service
## 网络可以启动失败
Wants=network-online.target

[Service]
## 又是这个, 发送消息才视为启动成功
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
## 调用dockerd, 传的参数还是看不懂
ExecStart=/usr/bin/dockerd -H unix:///var/run/docker.sock --selinux-enabled=false --default-ulimit nofile=65536:65536
## 重启还是貌似发的重启信号
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
## 看不懂喵
LimitNOFILE=infinity
## 看不懂喵
LimitNPROC=infinity
## 看不懂喵
LimitCORE=infinity
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
## 看不懂喵
TimeoutStartSec=0
# set delegate yes so that systemd does not reset the cgroups of docker containers
## 看不懂喵
Delegate=yes
# kill only the docker process, not all processes in the cgroup
## 看不懂喵
KillMode=process
# restart the docker process if it exits prematurely
## 看不懂喵
Restart=on-failure
## 看不懂喵
StartLimitBurst=3
## 看不懂喵
StartLimitInterval=60s

[Install]
## 在系统多用户启动时自启服务
WantedBy=multi-user.target
最后修改:2025 年 03 月 22 日
赛博讨口子