160 lines
5.6 KiB
Markdown
160 lines
5.6 KiB
Markdown
---
|
||
title: "systemd"
|
||
type: concept
|
||
tags: [linux, init, service-manager, ubuntu]
|
||
aliases: [systemd服务管理, systemd unit]
|
||
---
|
||
|
||
# systemd
|
||
|
||
## Overview
|
||
**systemd** 是 Linux 系统的服务管理器(初始化系统),是 Ubuntu Server、Debian、CentOS/RHEL、Fedora 等主流 Linux 发行版的默认初始化系统。systemd 通过 unit 文件(service、socket、timer 等)管理服务的生命周期,提供开机自启、自动重启、日志收集等生产级特性。
|
||
|
||
## Core Components
|
||
|
||
### Unit Types
|
||
| Type | Description | Example |
|
||
|------|-------------|---------|
|
||
| **service** | 后台守护进程 | frpc.service |
|
||
| **socket** | 监听 socket,按需激活服务 | ssh.socket |
|
||
| **timer** | 定时任务(cron 替代) | backup.timer |
|
||
| **mount** | 文件系统挂载点 | opt-frp.mount |
|
||
| **path** | 文件系统路径监控 | download.path |
|
||
|
||
### Core Commands
|
||
```bash
|
||
# 服务管理
|
||
systemctl start <service> # 启动服务
|
||
systemctl stop <service> # 停止服务
|
||
systemctl restart <service> # 重启服务
|
||
systemctl status <service> # 查看状态
|
||
systemctl enable <service> # 开机自启
|
||
systemctl disable <service> # 取消开机自启
|
||
systemctl enable --now <service> # 启用并立即启动
|
||
|
||
# 重新加载
|
||
systemctl daemon-reload # 重新加载 unit 文件
|
||
systemctl reload <service> # 热重载配置
|
||
|
||
# 查看日志
|
||
journalctl -u <service> # 查看服务日志
|
||
journalctl -u <service> -f # 实时流式日志
|
||
journalctl -u <service> -n 50 # 最近 50 行
|
||
journalctl --since "1 hour ago" -u <service> # 指定时间范围
|
||
```
|
||
|
||
## Service Unit Template
|
||
```ini
|
||
[Unit]
|
||
Description=<服务描述>
|
||
After=network.target # 网络就绪后启动
|
||
Wants=network-online.target # 等待网络完全上线
|
||
|
||
[Service]
|
||
Type=simple # 简单进程(推荐)
|
||
ExecStart=/path/to/binary # 启动命令
|
||
Restart=on-failure # 失败后自动重启
|
||
RestartSec=10 # 重启间隔 10 秒
|
||
User=<username> # 可选:指定运行用户
|
||
WorkingDirectory=/path # 可选:工作目录
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target # 多用户模式下启动
|
||
```
|
||
|
||
## Key Features
|
||
|
||
### 1. Automatic Restart (Restart=on-failure)
|
||
```ini
|
||
Restart=on-failure # 进程异常退出时重启(非 0 退出码)
|
||
Restart=always # 任何退出都重启
|
||
Restart=on-abnormal # 被信号终止时重启(SIGTERM/SIGKILL)
|
||
RestartSec=5 # 重启前等待 5 秒
|
||
```
|
||
**应用场景**:FRP 客户端连接中断后自动重连,无需手动干预。
|
||
|
||
### 2. Socket Activation (按需启动)
|
||
Ubuntu 24.04 SSH 默认使用 ssh.socket(按需激活):
|
||
```bash
|
||
# 查看 socket 状态
|
||
systemctl status ssh.socket
|
||
|
||
# 切换为传统 service 模式(持续运行)
|
||
sudo systemctl disable --now ssh.socket
|
||
sudo systemctl enable --now ssh.service
|
||
```
|
||
**优势**:无连接时节省资源,有连接时自动启动 sshd。
|
||
|
||
### 3. Journald Logging (日志管理)
|
||
systemd 集成 journald 日志系统,无需手动配置日志轮转:
|
||
- **自动日志轮转**:journald 自动管理日志大小
|
||
- **结构化日志**:支持 systemd-cat 写入结构化日志
|
||
- **二进制格式**:高效压缩,支持多种过滤条件
|
||
- **持久化存储**:设置 Storage=persistent 在 /var/log/journal/ 持久化
|
||
|
||
### 4. Dependencies (服务依赖)
|
||
```ini
|
||
After=network.target # 网络就绪后启动
|
||
After=network-online.target # 等待网络完全上线
|
||
Wants=network-online.target # 软依赖:网络上线后启动
|
||
Requires=postgresql.service # 强依赖:必须启动
|
||
PartOf=postgresql.service # 停止时级联停止
|
||
```
|
||
|
||
### 5. Environment Management
|
||
```ini
|
||
[Service]
|
||
Environment="FRP_TOKEN=secret123"
|
||
EnvironmentFile=/etc/frp/env # 从文件加载环境变量
|
||
```
|
||
|
||
## systemd vs Alternatives
|
||
|
||
| Feature | systemd | init (SysV) | OpenRC | runit |
|
||
|---------|---------|-------------|--------|-------|
|
||
| 开机自启 | systemctl enable | chkconfig | rc-update | ln -s |
|
||
| 进程监控 | 自动重启 | 需外部 watchdog | 外部 watchdog | supervise |
|
||
| 日志 | journalctl | 手动配置 | 手动配置 | 手动配置 |
|
||
| 并行启动 | 是 | 否 | 部分 | 是 |
|
||
| Socket 激活 | 是 | 否 | 否 | 否 |
|
||
| Timer 任务 | 是 | cron | cron | runit |
|
||
|
||
## Best Practices
|
||
|
||
1. **Type=simple**(推荐):单进程服务,systemd 直接监控主进程
|
||
2. **Restart=on-failure**:生产环境必备,防止进程崩溃后无人值守
|
||
3. **RestartSec**:设置合理间隔(如 10 秒),避免频繁重启
|
||
4. **daemon-reload**:修改 unit 文件后必须执行
|
||
5. **User=**:使用非 root 用户运行服务,降低安全风险
|
||
6. **ProtectSystem=**:限制服务对文件系统的访问范围
|
||
7. **ReadOnlyPaths=**:只读文件系统目录列表
|
||
8. **NoNewPrivileges=true**:防止服务提升权限
|
||
|
||
## FRP systemd Service Example
|
||
```ini
|
||
[Unit]
|
||
Description=frp client (frpc)
|
||
After=network-online.target
|
||
Wants=network-online.target
|
||
|
||
[Service]
|
||
Type=simple
|
||
ExecStart=/opt/frp/current/frpc -c /opt/frp/current/frpc.toml
|
||
Restart=on-failure
|
||
RestartSec=10
|
||
User=ubuntu
|
||
|
||
[Install]
|
||
WantedBy=multi-user.target
|
||
```
|
||
|
||
## Related Concepts
|
||
- [[launchd]] — macOS 原生服务管理器,与 systemd 功能对应但实现不同
|
||
- [[进程管理]] — systemd 是 Linux 进程管理的核心工具
|
||
- [[开机自启]] — systemd 的 enable 功能实现服务开机自启
|
||
- [[journald]] — systemd 集成的日志收集系统
|
||
|
||
## References
|
||
- Arch Wiki: https://wiki.archlinux.org/title/Systemd
|
||
- man pages: `man systemd.unit`, `man systemd.service`, `man systemctl`
|