如何快速檢查Linux系統(tǒng)中所有服務(wù)的狀態(tài)?
1. 使用systemctl命令(Systemd系統(tǒng))
對于基于Systemd的Linux發(fā)行版(如Ubuntu 16.04+、CentOS 7+),可通過以下命令列出所有服務(wù)狀態(tài):
systemctl list-units --type=service --all
過濾運(yùn)行中的服務(wù):
systemctl list-units --type=service --state=running
2. 通過service命令(SysVinit系統(tǒng))
在傳統(tǒng)SysVinit系統(tǒng)中,使用service --status-all
顯示所有服務(wù)狀態(tài):
service --status-all
結(jié)合管道符篩選結(jié)果:
service --status-all | grep -E '[ + ]'
3. 檢查服務(wù)啟動配置
使用chkconfig
查看服務(wù)在不同運(yùn)行級別的啟用狀態(tài):
chkconfig --list
對于Systemd系統(tǒng),查看服務(wù)是否開機(jī)啟動:
systemctl list-unit-files --type=service | grep enabled
4. 結(jié)合進(jìn)程監(jiān)控工具
使用ps
命令查看所有正在運(yùn)行的進(jìn)程:
ps aux | grep -E '^USER|systemd|init'
通過top
或htop
實(shí)時(shí)監(jiān)控服務(wù)資源占用情況。
5. 自動化腳本示例
創(chuàng)建腳本批量檢查服務(wù)狀態(tài)(Systemd系統(tǒng)):
#!/bin/bash
for service in $(systemctl list-units --type=service --no-legend | awk '{print $1}')
do
echo "檢查服務(wù): $service"
systemctl status $service | grep -E 'Active:|Loaded:'
done
注意事項(xiàng)
- 部分命令需要root權(quán)限,可使用
sudo
- 不同Linux發(fā)行版的服務(wù)管理工具可能不同
- 建議結(jié)合日志分析(
journalctl
)進(jìn)行深度排查