systemd
单元文件类型:
.service:定义系统服务.socket:定义套接字激活服务.device:定义硬件设备.mount和.automount:定义文件系统的挂载点.timer:定义类似 cron 的定时任务.target:将一组服务和其他单元逻辑地组合在一起,以达到某种系统状态(类似于传统的运行级别)
单元文件位置:
- 用户单元文件目录:
~/.config/systemd/user - 系统单元文件目录:
/etc/systemd/system - 包管理器安装的系统单元文件目录:
/usr/lib/systemd/system
占位符:
%h:用户主目录
Service
创建服务文件 /etc/systemd/system/example.service:
[Unit]
Description=My System Service
Requires=network.target
After=network.target
[Service]
Type=simple
DynamicUser=yes
ExecStart=/usr/bin/my-command
Restart=on-failure
RestartSec=5
LimitNOFILE=1048576
SyslogIdentifier=my-system-service
Environment="http_proxy=http://127.0.0.1:7890"
EnvironmentFile=/etc/my_service/environment
[Install]
WantedBy=default.target
After:指定服务单元在指定的其他单元启动之后再启动。常与Requires和After结合使用Requires:服务单元的强依赖,强依赖启动后服务才会启动Wants:服务单元的弱依赖,尝试启动弱依赖,然后启动服务DynamicUser=yes:指定服务在运行时动态创建一个用户和组Delegate=yes:允许服务管理自身的 cgroup 层级Restart:当服务进程意外退出时,是否自动重启服务no:默认值。服务退出后不会自动重启。always:无论服务是正常退出还是异常退出,都会自动重启。on-success:只有当服务正常退出(即退出状态码为零)时,才会自动重启。on-failure:只有当服务异常退出(即退出状态码非零、被信号终止等)时,才会自动重启。on-abnormal:只有当服务被信号终止或产生核心转储时,才会自动重启。on-abort:只有当服务被信号终止且该信号未被捕获时,才会自动重启。on-watchdog:只有当服务因看门狗超时被杀死时,才会自动重启。
RestartSec:指定服务在重启之前等待的时间间隔LimitNOFILE:服务进程可以打开的最大文件描述符数量SyslogIdentifier:指定服务的日志标识符ExecStart:服务启动命令Environment:设置环境变量EnvironmentFile:指定环境变量文件WantedBy:定义服务单元在启用(enable)时会被添加到哪些目标单元(target)
加载并启动服务
sudo systemctl daemon-reload # 重新加载 systemd 配置
sudo systemctl enable myservice.service # 允许开机自启
sudo systemctl start myservice.service # 启动服务
对于用户级服务,只需将
sudo systemctl替换为systemctl --user即可。
查看服务状态
sudo systemctl status myservice.service # 查看服务运行状态
sudo journalctl -eu myservice.service # 查看服务日志
-x:启用详细输出-e:跳转到日志的末尾-u:指定单元文件
Timer
Timer 负责在指定的时间启动对应的 service。
创建定时器单元文件 /etc/systemd/system/example.timer:
[Unit]
Description=Run My Daily Task at Midnight
[Timer]
OnCalendar=0:00
Persistent=true
[Install]
WantedBy=timers.target
创建同名服务单元文件 /etc/systemd/system/example.service:
[Unit]
Description=My Daily Task
[Service]
ExecStart=/path/to/your/script.sh
激活定时器:
systemctl enable example.timer
systemctl start example.timer
Slice
Slice 用于通过 cgroup 限制用户资源用量。
创建 slice 配置文件:/etc/systemd/system/user-.slice.d/50-defaults.conf:
[Unit]
Description=User Slice of UID %j
Documentation=man:user@.service(5)
StopWhenUnneeded=yes
[Slice]
# 限制 CPU 使用率
CPUQuota=200%
# 限制内存用量
MemoryHigh=3G
MemoryMax=4G
# 限制任务数量
TasksMax=1000
# IO 权重设置
IOWeight=100
重新加载配置:
sudo systemctl daemon-reload
参见:
- systemd | Arch Linux 中文维基
- Understanding Systemd Units and Unit Files | DigitalOcean
- Manage Systemd Services with systemctl on Linux | DigitalOcean
- Systemd Essentials: Working with Services, Units, and the Journal | DigitalOcean
- What is systemd's target, service and socket? | Stack Exchange
- systemd.service | Arch manual pages
Troubleshooting
Special user nobody configured, this is not safe!
如果服务使用了 nobody 用户,则会收到如下警告:
systemd[1]: /etc/systemd/system/frps.service:7: Special user nobody configured, this is not safe!
解决方法:使用 DynamicUser=yes

浙公网安备 33010602011771号