Golang 程序安装成系统服务实现开启自启与进程守护

  # 创建一个示例程序
  mkdir goApp
  cd goApp
  go mod init goApp
  touch main.go
  // main.go
  package main

  import (
      "net/http"

      "github.com/sohaha/zlsgo/zcli"
      "github.com/sohaha/zlsgo/zlog"
  )

  var (
      debug = zcli.SetVar("debug", "Debug mode").Bool()
  )

  func main() {
      // 设置应用信息
      zcli.Logo = `
  ____ __   ____   __  ____ ____
   (__  |  ) / ___) / _\(  _ (  _ \
   / _// (_/\___ \/    \) __/) __/
   (____)____(____/\_/\_(__) (__) `
      zcli.Version = "1.0.0"

      err := zcli.LaunchServiceRun("ZlsApp", "", run)

      if err == nil {
          stop()
      }

  }

  func run() {
      // 设置终端执行参数
      isDebug := *debug
      if isDebug {
          zlog.Dump("开发模式")
      }
      http.HandleFunc("/", Handler)
      http.ListenAndServe(":8888", nil)
  }

  func Handler(w http.ResponseWriter, r *http.Request) {

  }

  func stop() {

  }
  go build --o app

  # 查看帮助
  ./app --help

  # 安装成系统服务并且启动
  ./app install

讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!