systemctl status nginx看服务是否运行、主进程和最近日志。
约 1402 字大约 5 分钟
2026-05-14
systemctl 是管理 systemd 服务的入口。它负责查看服务状态、启动和停止服务、设置开机自启、重新加载 unit 配置。日常排查服务问题时,通常先用 systemctl 判断“服务有没有正常运行”。
systemctl 是管理 systemd 服务的入口。它负责查看服务状态、启动和停止服务、设置开机自启、重新加载 unit 配置。
systemctl status nginx看服务是否运行、主进程和最近日志。
sudo systemctl restart nginx修改配置后最常见的操作。
sudo systemctl reload nginx不中断主进程重新读取配置,前提是服务支持。
sudo systemctl enable --now nginx立即启动,同时设置开机自动启动。
sudo systemctl daemon-reload修改 service 文件后必须执行。
现代 Linux 发行版大多使用 systemd 管理系统服务。systemctl 操作的对象通常叫 unit,最常见的是 .service:
nginx.service
sshd.service
mysql.service
redis.service平时命令里可以省略 .service 后缀:
systemctl status nginx
systemctl status nginx.service这两条命令通常等价。
systemctl status nginx
systemctl is-active nginx
systemctl is-enabled nginx
systemctl list-units --type=service --state=running
systemctl list-unit-files --type=service几个命令的侧重点不同:
| 命令 | 关注点 | 适合什么时候用 |
|---|---|---|
status | 当前状态、PID、最近日志 | 排查服务是否正常 |
is-active | 是否正在运行 | 脚本里做判断 |
is-enabled | 是否开机自启 | 排查重启后服务没起来 |
list-units | 当前已加载的服务 | 看系统正在跑什么 |
list-unit-files | 所有已安装 unit 文件 | 找服务名、看 enable 状态 |
status 是最常用入口。看到 active (running) 只能说明进程还在,不代表业务一定可用;如果是 Web 服务,还要继续看端口、日志和健康检查。
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
sudo systemctl reload nginx
sudo systemctl try-reload-or-restart nginx| 操作 | 含义 | 风险 |
|---|---|---|
start | 启动服务 | 通常较低 |
stop | 停止服务 | 会中断服务 |
restart | 先停再起 | 会短暂中断连接 |
reload | 重新读取配置 | 不一定每个服务都支持 |
try-reload-or-restart | 能 reload 就 reload,否则 restart | 适合脚本化处理 |
修改配置后,不要直接重启。更稳妥的顺序是:先测试配置,再 reload 或 restart。比如 Nginx:
sudo nginx -t
sudo systemctl reload nginxsudo systemctl enable nginx
sudo systemctl disable nginx
sudo systemctl enable --now nginx
sudo systemctl disable --now nginxenable 只是设置开机自启,不一定会立刻启动;start 是立刻启动,但不一定开机自启。enable --now 则是两件事一起做。
| 目标 | 命令 |
|---|---|
| 现在启动 | sudo systemctl start nginx |
| 开机自动启动 | sudo systemctl enable nginx |
| 现在启动并开机自启 | sudo systemctl enable --now nginx |
| 停止并取消开机自启 | sudo systemctl disable --now nginx |
如果改了 /etc/systemd/system/*.service,systemd 不会自动读取新内容。通常要执行:
sudo systemctl daemon-reload
sudo systemctl restart your.service
sudo systemctl status your.service少了 daemon-reload,你可能以为配置改了,实际 systemd 还在使用旧版本。
如果只是修改应用自己的配置文件,例如 nginx.conf、redis.conf,通常不需要 daemon-reload,只需要按服务要求 reload 或 restart。
systemctl cat nginx
systemctl show nginx --property=FragmentPath,ExecStart,User,Group
systemctl list-dependencies nginx这些命令适合排查:
ExecStart 是什么;systemctl cat 尤其有用,因为它会把主 unit 和 drop-in 覆盖配置一起显示出来。
先看状态摘要:
systemctl status 服务名 --no-pager查看完整日志:
journalctl -u 服务名 -n 100 --no-pager如果刚改过 unit 文件,重新加载 systemd:
sudo systemctl daemon-reload如果是应用配置问题,先运行应用自己的配置检查命令。例如:
sudo nginx -t再决定 reload 还是 restart:
sudo systemctl reload 服务名
sudo systemctl restart 服务名| 现象 | 可能原因 | 下一步 |
|---|---|---|
Unit xxx.service not found | 服务名写错,或软件未安装 | `systemctl list-unit-files |
| 修改 service 后不生效 | 没有 daemon-reload | 重新加载后再重启 |
reload 失败 | 服务不支持重载 | 改用 restart,但注意中断 |
| 开机后服务没起来 | 没有 enable,或启动依赖失败 | is-enabled、journalctl -b -u 服务 |
| 服务显示 running 但访问失败 | 业务监听、端口、防火墙或配置问题 | 继续查 ss、日志、健康检查 |
不要把 restart 当万能修复
restart 能清掉一些临时状态,但也可能掩盖根因。生产环境里,重启前先看日志和配置变更,避免反复重启造成更大影响。
版权归属:Shuo Liu