Golang | Windows下增删改服务(调用 nssm 实现)

王先生
2024-07-04 / 0 评论 / 6 阅读 / 正在检测是否收录...
import (
    "fmt"
    "log"
    "os/exec"
)

/*
默认 是 自启动 开机自启
serviceName := "MyService"
executablePath := "C:\\path\\to\\my-service.exe"

    if err := installService(serviceName, executablePath); err != nil {
        log.Fatalf("Error installing service: %v", err)
    }
*/
func InstallService(nssmPath, serviceName, executablePath string) error {
    cmd := exec.Command(nssmPath, "install", serviceName, executablePath)
    err := cmd.Run()
    if err != nil {
        return fmt.Errorf("failed to install service: %w", err)
    }
    log.Printf("Service '%s' installed successfully.\n", serviceName)
    return nil
}

/*
serviceName := "MyService"
parameters := "--config=config.ini --log=service.log"

    if err := setAppParameters(serviceName, parameters); err != nil {
        log.Fatalf("Failed to set AppParameters: %v", err)
    }
*/
func SetAppParameters(nssmPath, serviceName, parameters string) error {
    cmd := exec.Command(nssmPath, "set", serviceName, "AppParameters", parameters)
    err := cmd.Run()
    if err != nil {
        return err
    }
    log.Printf("AppParameters for service '%s' set to '%s'.\n", serviceName, parameters)
    return nil
}

// 设置服务的工作目录
/*
if err := setAppDirectory(serviceName, directory); err != nil {
        log.Fatalf("Failed to set AppDirectory: %v", err)
    }
*/
func SetAppDirectory(nssmPath, serviceName, directory string) error {
    cmd := exec.Command(nssmPath, "set", serviceName, "AppDirectory", directory)
    err := cmd.Run()
    if err != nil {
        return err
    }
    log.Printf("AppDirectory for service '%s' set to '%s'.\n", serviceName, directory)
    return nil
}

// 设置服务的标准输出文件
/*
    if err := setAppStdout(serviceName, stdoutFile); err != nil {
        log.Fatalf("Failed to set AppStdout: %v", err)
    }
*/
func SetAppStdout(nssmPath, serviceName, filePath string) error {
    cmd := exec.Command(nssmPath, "set", serviceName, "AppStdout", filePath)
    err := cmd.Run()
    if err != nil {
        return err
    }
    log.Printf("AppStdout for service '%s' set to '%s'.\n", serviceName, filePath)
    return nil
}

// 设置服务的标准错误输出文件
/*
if err := setAppStderr(serviceName, stderrFile); err != nil {
        log.Fatalf("Failed to set AppStderr: %v", err)
    }
*/
func SetAppStderr(nssmPath, serviceName, filePath string) error {
    cmd := exec.Command(nssmPath, "set", serviceName, "AppStderr", filePath)
    err := cmd.Run()
    if err != nil {
        return err
    }
    log.Printf("AppStderr for service '%s' set to '%s'.\n", serviceName, filePath)
    return nil
}

/*
// 在main函数中调用

    if err := startService(serviceName); err != nil {
        log.Fatalf("Error starting service: %v", err)
    }
*/
func StartService(serviceName string) error {
    cmd := exec.Command("nssm", "start", serviceName)
    err := cmd.Run()
    if err != nil {
        return fmt.Errorf("failed to start service: %w", err)
    }
    log.Printf("Service '%s' started successfully.\n", serviceName)
    return nil
}

/*
*
// 在main函数中调用

    if err := stopService(serviceName); err != nil {
        log.Fatalf("Error stopping service: %v", err)
    }
*/
func StopService(serviceName string) error {
    cmd := exec.Command("nssm", "stop", serviceName)
    err := cmd.Run()
    if err != nil {
        return fmt.Errorf("failed to stop service: %w", err)
    }
    log.Printf("Service '%s' stopped successfully.\n", serviceName)
    return nil
}

/*
*
// 在main函数中调用
if err := uninstallService(serviceName); err != nil {
log.Fatalf("Error removing service: %v", err)
}
*/
func UninstallService(serviceName string) error {
    cmd := exec.Command("nssm", "remove", serviceName, "confirm")
    err := cmd.Run()
    if err != nil {
        return fmt.Errorf("failed to remove service: %w", err)
    }
    log.Printf("Service '%s' removed successfully.\n", serviceName)
    return nil
}

评论 (0)

取消