Files
nexus/wiki/concepts/Socket-Activation.md
2026-04-16 21:08:55 +08:00

46 lines
1.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Socket Activation"
type: concept
tags: [systemd, activation, optimization]
date: 2026-04-16
---
## Definition
Socket Activation套接字激活是 systemd 的一种按需启动机制,只有当客户端首次请求连接时,服务才会被启动。与传统的开机自启相比,可以减少系统资源占用。
## Mechanism
1. systemd 创建监听套接字
2. 客户端发起连接请求
3. systemd 接收连接并启动服务进程
4. 连接传递给已启动的服务
## Use Cases
- SSHssh.socket
- D-Bus 系统消息总线
- 打印服务cups.socket
## Advantages
- 减少内存占用
- 按需启动,节能
- 简化服务依赖管理
## Comparison with Traditional Mode
| 特性 | Socket Activation | 传统开机自启 |
|------|------------------|-------------|
| 启动时机 | 按需启动 | 开机启动 |
| 资源占用 | 低 | 常驻内存 |
| 响应速度 | 首次延迟 | 即时响应 |
## Example
```bash
# 启用 socket 激活(默认)
sudo systemctl enable ssh.socket
# 切换回传统模式
sudo systemctl disable ssh.socket
sudo systemctl enable ssh.service
```
## Connections
- [[systemd]] ← implements ← [[Socket Activation]]
- [[SSH]] ← uses ← [[Socket Activation]]