# 开机自启（服务）

# Axigen 在 Debian (systemd) 上自启动问题完整解决方案

## 📌 问题背景

在 Debian（systemd）环境中，Axigen 安装后可以通过：

```bash
/etc/init.d/axigen start
```

正常启动，但在 VPS 重启后不会自动运行。

---

## ❗ 根本原因

- Axigen 仅提供 init.d 脚本
- 未提供 systemd service
- systemd 无法感知 Axigen 进程

直接导致：

- 无法开机自启
- systemctl 无法管理服务

---

## ❌ 错误尝试总结

### 1. 直接使用 systemd 启动二进制

```ini
ExecStart=/opt/axigen/bin/axigen
```

问题：

- 缺少初始化环境
- 无法加载配置
- 启动失败（exit 255）

---

### 2. 使用 run 目录配置

```ini
-c /var/opt/axigen/run/axigen.cfg
```

问题：

- run 目录为运行时动态生成
- 不能用于冷启动

---

### 3. PID 文件方式

```ini
PIDFile=/var/run/axigen.pid
```

问题：

- 实际不存在 PID 文件
- systemd 状态错误

---

## ✅ 最终解决方案（推荐）

采用：

> **systemd + init.d（Hybrid 模式）**

---

## 🧩 service 配置

路径：

```bash
/etc/systemd/system/axigen.service
```

内容：

```ini
[Unit]
Description=Axigen Mail Server (Hybrid)
After=network-online.target
Wants=network-online.target

[Service]
Type=forking

ExecStart=/etc/init.d/axigen start
ExecStop=/etc/init.d/axigen stop
ExecReload=/etc/init.d/axigen restart

Restart=on-failure
RestartSec=5

# 防止频繁重启
StartLimitInterval=60
StartLimitBurst=3

[Install]
WantedBy=multi-user.target
```

---

## 🚀 启用步骤

```bash
systemctl daemon-reload
systemctl enable axigen
systemctl reset-failed axigen
systemctl start axigen
```

---

## 🔍 验证状态

```bash
systemctl status axigen
```

成功状态：

```text
Active: active (running)
```

---

## 🔁 开机自启测试

```bash
reboot
```

重启后：

```bash
systemctl status axigen
```

---

## 🧠 架构说明

| 组件 | 作用 |
|------|------|
| systemd | 生命周期管理 |
| init.d | 初始化环境 |

---

## ⚠️ 注意事项

- 不要直接运行 axigen 二进制
- 不要使用 run 目录 cfg 做冷启动
- 不要依赖 PID 文件

---

## 🔥 常见错误

### already running

原因：已有进程但 systemd 不知道

解决：

```bash
pkill -9 axigen
systemctl reset-failed axigen
```

---

### exit 255

原因：缺少初始化环境或配置错误

---

## ✅ 最终结论

在 Debian + systemd 环境中：

> 使用 Hybrid（systemd + init.d）是 Axigen 最稳定方案

---
