Systemd服务管理与自启配置


Systemd 是现代 Linux 系统的初始化系统和服务管理器,CentOS 7+、Ubuntu 16.04+、Debian 8+ 均默认使用。在搬瓦工VPS上,Nginx、MySQL、Redis 等服务都通过 Systemd 管理。掌握 systemctl 命令和自定义 service 文件的编写,是VPS运维的基本功。

一、systemctl 常用命令

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文件后必须执行)

二、service 文件结构

Systemd 的服务配置文件(Unit文件)通常存放在以下位置:

一个完整的 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

四、Journal 日志查看

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 参数可按级别过滤。

五、常见问题排查

服务启动失败的排查步骤:

  1. 查看详细状态:systemctl status myapp -l
  2. 查看完整日志:journalctl -u myapp --no-pager
  3. 检查service文件语法:systemd-analyze verify /etc/systemd/system/myapp.service
  4. 手动执行 ExecStart 中的命令,确认能正常运行
  5. 检查文件权限和用户权限是否正确

常见错误及解决:

六、实用技巧

Tip: 购买搬瓦工VPS时使用优惠码 NODESEEK2026 可享 6.77% 折扣,详见优惠码使用教程。更多教程请查看新手教程
关于本站

搬瓦工VPS中文网(bwgvps.com)是非官方中文信息站,整理搬瓦工 BandwagonHost 的优惠信息、使用教程和方案对比,方便中文用户选购和使用。

新手必读
搬瓦工优惠码

当前最大折扣优惠码:

NODESEEK2026(优惠 6.77%)

在购买方案时填入优惠码即可自动抵扣。详见 优惠码使用教程