Systemd .service Generator
Generate a standard systemd unit file visually — set exec path, working dir, user and env vars, then pick auto-start and crash-restart to get a ready .service and systemctl commands
基本信息
守护选项
启动顺序依赖(After)
常见模板(点击填入)
myapp.service
# 由 VPS-Craft 生成的 systemd 单元文件 # 保存为 /etc/systemd/system/myapp.service [Unit] Description=myapp service After=network.target network-online.target Wants=network-online.target [Service] Type=simple User=appuser ExecStart=/usr/local/bin/myapp --port 8080 --host 0.0.0.0 Restart=always RestartSec=5 StandardOutput=journal StandardError=journal [Install] WantedBy=multi-user.target
一键执行命令
sudo cp myapp.service /etc/systemd/system/ sudo systemctl daemon-reload sudo systemctl enable --now myapp sudo systemctl status myapp sudo journalctl -u myapp -f
Systemd 小知识
- 改完要 daemon-reload 每次修改 .service 文件后都要执行 systemctl daemon-reload 才能生效;enable --now 会同时设为开机自启并立即启动。
- Restart=always 保活 设为 always 后进程不论因何种原因退出都会被拉起;配合 RestartSec 可避免崩溃时疯狂重启。on-failure 只在非零退出时重启,适合一次性或预期退出的任务。
- 别用 root 跑业务 生产环境建议新建专用低权限用户(如 appuser)并通过 User= 指定,降低被入侵后的影响面。
- 日志看这里 StandardOutput/Error 设为 journal 后,用 journalctl -u 服务名 -f 实时跟踪日志,无需自己写日志文件。
- 环境变量带空格要加引号 Environment= 的值若含空格,生成器会自动加引号(如 Environment="KEY=value with space"),否则 systemd 会解析错误。