查看服务列表

查看运行中的服务

1
systemctl list-units --type=service --state=running

查看所有服务(包括未启动)

1
systemctl list-units --type=service --all

查看服务状态摘要

1
systemctl list-unit-files --type=service

查看失败的服务

1
systemctl --failed

管理服务

查看特定服务状态

1
systemctl status <service_name>.service

启用/禁用开机自启

1
2
3
4
5
# 启用
systemctl enable <service_name>.service

# 禁用
systemctl disable <service_name>.service

PostgreSQL 服务管理

停止服务

1
sudo systemctl stop postgresql

禁用开机自启

1
sudo systemctl disable postgresql

重启服务

1
sudo systemctl restart postgresql

查看状态

1
sudo systemctl status postgresql

多版本 PostgreSQL

查找所有 PostgreSQL 服务:

1
sudo systemctl list-units --type=service | grep postgres

管理特定版本:

1
2
3
4
5
# 停止
sudo systemctl stop postgresql@10-main

# 禁用
sudo systemctl disable postgresql@10-main

常用命令对照表

操作 命令
启动服务 systemctl start <service>
停止服务 systemctl stop <service>
重启服务 systemctl restart <service>
重载配置 systemctl reload <service>
查看状态 systemctl status <service>
启用开机自启 systemctl enable <service>
禁用开机自启 systemctl disable <service>

注意:大部分 systemctl 命令需要 sudo 权限。