systemctl status 是首选;脚本里用 is-active/is-enabled 判断;老系统用 service –status-all。以下均经 WSL2 实测。
一、TL;DR:按场景选命令
- 交互查看单个服务:
systemctl status <服务>,含进程、内存、最近日志。 - 脚本判断运行/开机自启:
systemctl is-active/is-enabled,看退出码。 - 批量看所有服务:
systemctl list-units --type=service或list-unit-files。 - 老式 SysV 脚本:
service --status-all,输出[ + ]/[ - ]。 - 看进程与日志:
pgrep/ps定位进程,journalctl -u看日志。
二、systemctl status:首选,含日志
systemctl status cron --no-pager
systemctl status cron --no-pager -l
systemctl status cron --no-pager --lines=20
实测输出(节选):
● cron.service - Regular background program processing daemon
Loaded: loaded (.../cron.service; enabled; preset: enabled)
Active: active (running) since Thu 2026-11-05 00:44:16 CST
Docs: man:cron(8)
Main PID: 159 (cron)
Tasks: 1
Memory: 492.0K
systemctl status 从 systemd 取运行时状态,并附带最近 journal 日志 [1]。默认只显示 10 行并截断长行;--full 不截断,--lines=N 调整行数,-l 等价于 --full。服务名可省略 .service 后缀,systemd 会自动补全。
三、is-active / is-enabled:脚本与判断
systemctl is-active cron; echo "exit=$?"
systemctl is-enabled cron; echo "exit=$?"
systemctl is-failed cron; echo "exit=$?"
is-active:至少一个 unit 处于 active 状态返回 0,否则非 0;默认打印状态词。is-enabled:至少一个 unit 文件处于 enabled 状态返回 0;输出见 man 表(enabled/enabled-runtime/masked 等)。is-failed:检查 failed 状态,配合--quiet只取退出码。
脚本里加 --quiet 避免输出干扰:
if systemctl is-active --quiet cron; then echo "running"; fi
三者都支持 glob 模式匹配多个 unit [1]。
四、list-units / list-unit-files:批量查看
systemctl list-units --type=service --state=running --no-pager --no-legend
systemctl list-units --type=service --state=failed --no-pager
systemctl list-unit-files --type=service --no-pager | grep -i cron
list-units:看当前已加载 unit 的运行状态,支持--all显示 inactive。list-unit-files:看已安装 unit 文件及其 enablement 状态(enabled/disabled/static/masked)。
两者都支持 --type=、--state= 过滤;list-units 默认只显示 active,加 --all 才显示全部 [1]。
五、service、ps、pgrep、journalctl:兼容与进程日志
service --status-all
service cron status
pgrep -a cron
ps -o pid,comm,args -C cron
journalctl -u cron --no-pager -n 10
service --status-all:按字母顺序对所有 init 脚本执行 status,[ + ]运行、[ - ]停止、[ ? ]无 status 命令;仅对 SysV 任务有效 [2]。service <name> status:对 systemd unit 会透传到 systemctl 等价命令。pgrep -a/ps -C:按进程名定位 PID,不依赖 systemd。journalctl -u <服务>:查看该 unit 的日志,支持--since/--until时间过滤 [3]。
六、回滚、排错与局限性
排错速查
| 现象 | 原因 | 处理 | |
|---|---|---|---|
Unit xxx.service could not be found. |
未安装或 unit 名拼写错误 | `systemctl list-unit-files \ | grep` 确认名称 |
| status 显示 inactive (dead) | 服务未启动或已退出 | systemctl start 后再 status |
|
is-enabled 输出 masked |
unit 被屏蔽 | systemctl unmask 解除 |
|
service --status-all 无输出或报错 |
纯 systemd 环境无 SysV 脚本 | 改用 systemctl list-units |
|
| journalctl 无权限看系统日志 | 非 root 且不在 adm/systemd-journal 组 | 加入组或用 sudo | |
| 进程存在但 systemctl 不认识 | 服务未注册为 unit | 用 ps/pgrep 定位,或补写 unit 文件 |
局限性
- WSL2 下 systemd 需
systemd=true(/etc/wsl.conf)才能运行;本环境已启用。 service --status-all仅覆盖 SysV 任务,systemd 原生服务不会列出。- 容器里常无 systemd,
systemctl会报错,改用ps/pgrep或直接查 PID 文件。 is-failed在部分版本会打印状态词而非静默,脚本务必加--quiet。
实测记录
- 环境:WSL2 Ubuntu 24.04.4 LTS,内核 6.6.114.1-microsoft-standard-WSL2,systemd 运行中。
systemctl status cron:输出 active (running),Main PID 159,内存 492K。is-active/is-enabled:均返回 0,输出 active/enabled。service --status-all:cron 显示[ + ]。list-unit-files:cron.service 为 enabled。pgrep -a cron:PID 159,命令行 /usr/sbin/cron -f -P。journalctl -u cron -n 5:正常输出启动日志。
参考资料
- [1] systemctl(1) – Linux manual page (man7.org)(systemctl(1))
- [2] service(8) — Debian bookworm 手册页(service(8))
- [3] journalctl(1) – Linux manual page (man7.org)(journalctl(1))
文中主要命令已在 WSL Ubuntu(24.04)实测并核对其在 Debian 系与通用发行版的文档口径。