最准的是systemctl status 服务名,可查看active状态、main pid及错误日志;is-active适合脚本判断;list-units --type=service --state=running列出所有真正在运行的服务。

直接看服务当前是否在跑,最准的是 systemctl status 服务名。它不只是告诉你“开”或“关”,还能看到主进程有没有起来、最近有没有报错、配置是否加载成功——这些才是判断服务是否真正可用的关键。
用 systemctl status 查单个服务的实时状态
这个命令输出的信息最全,适合排查问题:
- 执行
systemctl status nginx(把 nginx 换成你要查的服务名) - 关注三处:
-
Active:后面是不是active (running);如果只是active (exited),说明服务已退出,可能根本没在干活 -
Main PID:后面有没有真实的数字,且对应进程State:是running,不是exited或dead - 最后几行日志里有没有
Failed to start、Permission denied、Connection refused这类关键词
-
用 systemctl is-active 快速判断运行与否
适合写脚本或快速验证,不带多余信息:
- 运行
systemctl is-active sshd - 返回
active表示正在运行,inactive表示停着,failed表示启动失败 - 加上
--quiet参数可静默执行,靠$?退出码做逻辑判断(0=active,非0=其他)
列出所有当前运行中的服务
想一眼看清系统里哪些服务真正在工作:
- 执行
systemctl list-units --type=service --state=running - 输出只包含
active (running)和activating状态的服务,不含已停止或失败项 - 可加
-n 20限制显示行数,避免刷屏
区分“现在在跑”和“开机要自启”
这两个概念常被混淆:
-
systemctl is-enabled nginx看的是开机自启配置,返回enabled/disabled/static - 即使返回
enabled,服务也可能当前没运行;反过来,disabled的服务也能手动启动
别依赖 ps aux | grep nginx 或 service --status-all。前者容易漏掉 socket 类服务、一次性服务,后者在 systemd 系统中信息混乱、不准确。systemd 的状态抽象才是权威来源。











