fix: 修复安装/升级无法重启服务的问题

This commit is contained in:
shaw
2025-12-18 17:44:49 +08:00
parent ef3199f0ca
commit 8e4bd42e8c
4 changed files with 63 additions and 50 deletions

View File

@@ -2,17 +2,15 @@ package setup
import (
"fmt"
"log"
"net/http"
"net/mail"
"os/exec"
"regexp"
"runtime"
"strings"
"sync"
"time"
"sub2api/internal/pkg/response"
"sub2api/internal/pkg/sysutil"
"github.com/gin-gonic/gin"
)
@@ -346,7 +344,7 @@ func install(c *gin.Context) {
go func() {
// Wait a moment to ensure the response is sent
time.Sleep(500 * time.Millisecond)
triggerServiceRestart()
sysutil.RestartServiceAsync()
}()
response.Success(c, gin.H{
@@ -355,27 +353,3 @@ func install(c *gin.Context) {
})
}
// triggerServiceRestart attempts to restart the service via systemd
// This is called after setup completes to switch from setup mode to normal mode
func triggerServiceRestart() {
if runtime.GOOS != "linux" {
log.Println("Service restart: not on Linux, manual restart required")
return
}
log.Println("Setup completed, triggering service restart...")
// Try direct systemctl first (works if running as root or with proper permissions)
cmd := exec.Command("systemctl", "restart", "sub2api")
if err := cmd.Run(); err != nil {
// Try with sudo (requires NOPASSWD sudoers entry)
sudoCmd := exec.Command("sudo", "systemctl", "restart", "sub2api")
if sudoErr := sudoCmd.Run(); sudoErr != nil {
log.Printf("Service restart failed: %v (sudo also failed: %v)", err, sudoErr)
log.Println("Please restart the service manually: sudo systemctl restart sub2api")
return
}
}
log.Println("Service restart initiated successfully")
}