Systemd 是现代 Linux 系统的初始化系统和服务管理器,CentOS 7+、Ubuntu 16.04+、Debian 8+ 均默认使用。在搬瓦工VPS上,Nginx、MySQL、Redis 等服务都通过 Systemd 管理。掌握 systemctl 命令和自定义 service 文件的编写,是VPS运维的基本功。
systemctl 是管理 Systemd 服务的核心命令。
服务管理:
| 命令 | 说明 |
|---|---|
systemctl start nginx | 启动服务 |
systemctl stop nginx | 停止服务 |
systemctl restart nginx | 重启服务 |
systemctl reload nginx | 重新加载配置(不中断服务) |
systemctl status nginx | 查看服务状态 |
systemctl enable nginx | 设置开机自启 |
systemctl disable nginx | 取消开机自启 |
systemctl is-active nginx | 检查服务是否运行 |
systemctl is-enabled nginx | 检查是否开机自启 |
查看服务列表:
# 查看所有运行中的服务 systemctl list-units --type=service --state=running # 查看所有已启用的服务 systemctl list-unit-files --type=service --state=enabled # 查看所有失败的服务 systemctl list-units --type=service --state=failed
系统管理:
| 命令 | 说明 |
|---|---|
systemctl reboot | 重启系统 |
systemctl poweroff | 关机 |
systemctl daemon-reload | 重新加载 Systemd 配置(修改service文件后必须执行) |
Systemd 的服务配置文件(Unit文件)通常存放在以下位置:
/usr/lib/systemd/system/ -- 系统安装的服务(软件包自带)/etc/systemd/system/ -- 自定义服务文件(优先级更高)一个完整的 service 文件包含三个部分:
[Unit] Description=服务描述 After=network.target # 在网络服务启动后再启动 Wants=network-online.target [Service] Type=simple # 服务类型 User=www # 运行用户 Group=www # 运行用户组 WorkingDirectory=/opt/app # 工作目录 ExecStart=/opt/app/start.sh # 启动命令 ExecStop=/opt/app/stop.sh # 停止命令(可选) ExecReload=/bin/kill -HUP $MAINPID # 重载命令(可选) Restart=on-failure # 异常退出时自动重启 RestartSec=5 # 重启间隔(秒) StandardOutput=journal # 标准输出到journal StandardError=journal # 错误输出到journal [Install] WantedBy=multi-user.target # 多用户模式下启用
Type 字段说明:
| 类型 | 说明 | 适用场景 |
|---|---|---|
simple | 默认值,ExecStart启动的进程就是主进程 | 大多数应用 |
forking | 进程会fork子进程,父进程退出后子进程成为主进程 | 传统daemon程序 |
oneshot | 一次性任务,执行完就退出 | 初始化脚本 |
notify | 进程启动完毕后会发送通知 | 支持sd_notify的程序 |
Restart 策略:
| 值 | 说明 |
|---|---|
no | 不自动重启(默认) |
on-failure | 非正常退出时重启(推荐) |
always | 无论何种原因退出都重启 |
on-abnormal | 信号终止或超时时重启 |
示例1:Node.js 应用
创建文件 /etc/systemd/system/myapp.service:
[Unit] Description=My Node.js Application After=network.target [Service] Type=simple User=www WorkingDirectory=/opt/myapp ExecStart=/usr/bin/node /opt/myapp/server.js Restart=on-failure RestartSec=10 Environment=NODE_ENV=production Environment=PORT=3000 [Install] WantedBy=multi-user.target
示例2:Python Flask/Django 应用(Gunicorn)
[Unit] Description=Gunicorn WSGI Server After=network.target [Service] Type=simple User=www Group=www WorkingDirectory=/opt/webapp ExecStart=/opt/webapp/venv/bin/gunicorn -w 4 -b 0.0.0.0:8000 app:app Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target
示例3:自定义脚本定时运行(配合Timer)
创建服务文件 /etc/systemd/system/backup.service:
[Unit] Description=Daily Backup Script [Service] Type=oneshot ExecStart=/root/scripts/backup.sh
创建定时器文件 /etc/systemd/system/backup.timer:
[Unit] Description=Run backup daily at 3am [Timer] OnCalendar=*-*-* 03:00:00 Persistent=true [Install] WantedBy=timers.target
启用自定义服务:
# 重新加载 Systemd 配置 systemctl daemon-reload # 启动服务 systemctl start myapp # 设置开机自启 systemctl enable myapp # 查看状态 systemctl status myapp
Systemd 使用 journalctl 统一管理日志,替代传统的 syslog。
| 命令 | 说明 |
|---|---|
journalctl -u nginx | 查看指定服务的日志 |
journalctl -u nginx -f | 实时跟踪日志输出 |
journalctl -u nginx --since today | 查看今天的日志 |
journalctl -u nginx --since "2024-01-01" --until "2024-01-02" | 按时间范围查看 |
journalctl -u nginx -n 50 | 查看最近50条日志 |
journalctl -u nginx -p err | 只看错误级别日志 |
journalctl --disk-usage | 查看日志占用磁盘空间 |
journalctl --vacuum-size=500M | 限制日志最大占用500MB |
journalctl --vacuum-time=7d | 清理7天前的日志 |
日志级别从高到低:emerg, alert, crit, err, warning, notice, info, debug。使用 -p 参数可按级别过滤。
服务启动失败的排查步骤:
systemctl status myapp -ljournalctl -u myapp --no-pagersystemd-analyze verify /etc/systemd/system/myapp.service常见错误及解决:
chmod +x script.shsystemctl daemon-reloadsystemctl edit nginx 创建 override 文件,不直接修改原始service文件systemctl list-dependencies nginxsystemd-analyze blame 查看各服务的启动时间systemctl mask nginx 完全阻止服务启动,systemctl unmask nginx 解除Tip: 购买搬瓦工VPS时使用优惠码 NODESEEK2026 可享 6.77% 折扣,详见优惠码使用教程。更多教程请查看新手教程。