Systemd 是 Linux 系统的一种初始化系统实现,它管理的各种服务叫做 Unit,每个服务对应一个 Unit 配置文件

Systemd 的服务有两种状态:

  1. 运行中/已停止/错误
  2. 启用/禁用

Systemd 的 Unit 配置文件 通常位于这些目录中
/lib/systemd/system (供软件包使用)
/etc/systemd/system (供管理员使用)

本示例以 natfrp为例

创建程序的信息目录

mkdir -p /usr/local/etc/natfrp

创建文件

nano /etc/systemd/system/frpc@.service

填充内容,并且保存

[Unit]
Description=SakuraFrp Service
After=network.target

[Service]
Type=idle
User=nobody
Restart=on-failure
RestartSec=60s
ExecStart=/usr/local/bin/frpc -f %i
WorkingDirectory=/usr/local/etc/natfrp

[Install]
WantedBy=multi-user.target

重载 Systemd,这样服务就配置完成

systemctl daemon-reload

启动/停止

systemctl <start|stop> <Unit名称>

查看状态

systemctl status <Unit名称>

运行中的

systemctl list-units "frpc@*"

查看日志

journalctl -u <Unit名称>

配置开机自启

systemctl <enable|disable> <Unit名称>
systemctl status <Unit名称>

查询以前的

systemctl list-units --all "frpc@*"