第 8 章 · 01 :服务的生杀大权 本节摘要:现代 Linux 几乎都用 systemd 作为初始化系统(PID 1),所以 是你管理服务的核心命令。本节要讲透 的两组关键语义:一是「立即启停」( / / )与「开机自启开关」( / )是两件独立的事——这是新手最容易混淆的点;二是怎么读懂 的输出(绿色 active 还是红色 failed)。掌握这两组语义,你就能让服务「现在就跑」且「重启后还跑」。 内容来源:综合 systemd 与 知识整理,套用体系化模板。 学习目标 阅读完本节,你应当能够: 用 立即控制服务运行状态。 用 控制服务是否开机自启。 说清「立即启停」与「开机自启」是两套独立语义(最易混淆点)。
systemctl:服务的生杀大权本节摘要:现代 Linux 几乎都用 systemd 作为初始化系统(PID 1),所以
systemctl是你管理服务的核心命令。本节要讲透systemctl的两组关键语义:一是「立即启停」(start/stop/restart)与「开机自启开关」(enable/disable)是两件独立的事——这是新手最容易混淆的点;二是怎么读懂systemctl status的输出(绿色 active 还是红色 failed)。掌握这两组语义,你就能让服务「现在就跑」且「重启后还跑」。
内容来源:综合 systemd 与
systemctl知识整理,套用体系化模板。
阅读完本节,你应当能够:
systemctl start/stop/restart 立即控制服务运行状态。systemctl enable/disable 控制服务是否开机自启。systemctl status 输出:active(运行中)、inactive(未运行)、failed(失败)。systemctl list-units/list-unit-files 查看全系统服务状态。systemctl 最让人混淆的,是它有两组看起来类似、实则完全独立的语义:
立即启停组(影响「现在」):
start —— 现在就启动服务stop —— 现在就停止服务restart —— 现在就重启(先 stop 再 start)reload —— 重新加载配置(不重启,比 restart 温和)开机自启组(影响「下次开机」):
enable —— 下次开机时自动启动disable —— 下次开机时不自动启动这两组互不影响。一个服务可以「enable 了但没 start」(开机自启,但现在没跑),也可以「start 了但没 enable」(现在在跑,但重启后就没了)。新手常犯的错是只 start 没 enable,结果重启机器服务就消失了。
💡 技巧:部署新服务时,通常
start+enable都要做:enable --now一条命令搞定启动并设置开机自启。
systemctl enable --now nginx # 启动 + 设置开机自启(一步到位) systemctl disable --now nginx # 停止 + 取消开机自启
sudo systemctl start nginx # 启动 sudo systemctl stop nginx # 停止 sudo systemctl restart nginx # 重启(有短暂中断) sudo systemctl reload nginx # 重载配置(通常无中断, 但需服务支持)
reload vs restart:reload 让服务重新读配置但不重启进程(更平滑,无连接中断),restart 完全重启(有中断)。reload 需要服务自己支持(Nginx、Apache 支持,有些服务不支持只能 restart)。
sudo systemctl enable nginx # 开机自启 sudo systemctl disable nginx # 取消开机自启 sudo systemctl is-enabled nginx # 查是否设了自启
systemctl status nginx # 查详细状态(运行状态 + 最近日志) systemctl is-active nginx # 只查运行中否(active/inactive) systemctl is-enabled nginx # 只查自启否(enabled/disabled) systemctl is-failed nginx # 查是否失败
$ systemctl status nginx ● nginx.service - The nginx HTTP and reverse proxy server Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled) Active: active (running) since Tue 2026-08-04 10:00:00 UTC; 1h ago Main PID: 1234 (nginx) Tasks: 3 Memory: 5.2M CPU: 100ms CGroup: /system.slice/nginx.service ├─1234 nginx: master process └─1235 nginx: worker process Aug 04 10:00:00 server systemd[1]: Started nginx...
关键信息:
Active: active (running) —— 服务正在跑(绿色);inactive 表示没跑;failed(红色)表示失败。Loaded: ... enabled —— 配置已加载,enabled 表示设了开机自启。Main PID: 1234 —— 主进程 PID。systemctl list-units --type=service # 所有已加载的服务 systemctl list-unit-files --type=service # 所有服务(含未加载的) systemctl list-units --type=service --state=running # 只看在跑的 systemctl --failed # 看所有失败的服务
sudo systemctl start nginx # 现在跑起来了 # 重启机器后 nginx 没自动启动! sudo systemctl enable nginx # 别忘了 enable(或用 enable --now)
部署服务务必「start + enable」都做。
Active: failed (Result: exit-code)
failed 说明服务启动失败了。看日志:journalctl -u nginx -e(下一节专讲 journalctl),或 systemctl status nginx 底部的日志行,通常能看到具体报错(配置语法错、端口被占、权限不足)。
sudo systemctl reload nginx # nginx 支持, OK sudo systemctl reload someapp # 报错: "Job type reload is not supported"
不是所有服务都支持 reload。不支持时只能 restart(有短暂中断)。
systemctl status nginx # 可以(自动补 .service) systemctl status nginx.service # 也行(显式)
.service 后缀可省略,systemctl 会自动补全。但 .timer/.socket 等其他类型必须写全。
start/stop/restart(立即启停)与 enable/disable(开机自启)互不影响。enable --now 一条命令完成「启动 + 开机自启」,部署新服务推荐。reload vs restart:reload 不重启进程、无中断(需服务支持);restart 完全重启、有中断。status 读懂:Active: active (running) 正跑、inactive 没跑、failed 失败;Loaded: ... enabled 已设自启。journalctl -u 服务名 -e(下一节)或 status 底部日志。.service 后缀可省,但 .timer/.socket 必须写全。下一节讲统一日志查询——journalctl,出问题时它和 systemctl 是一对搭档。