From 2a5e90caeb2ce53b310e8024cac4339bb46a0f31 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Tue, 12 Jan 2021 10:56:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=B3=BB=E7=BB=9F=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/edge-node/main.go | 50 ++++++++++++++++++++++++++++++- internal/nodes/system_services.go | 7 +++++ internal/utils/service_linux.go | 4 +-- 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/cmd/edge-node/main.go b/cmd/edge-node/main.go index 6c182cd..a12bd45 100644 --- a/cmd/edge-node/main.go +++ b/cmd/edge-node/main.go @@ -7,17 +7,22 @@ import ( "github.com/TeaOSLab/EdgeNode/internal/nodes" "github.com/iwind/TeaGo/Tea" _ "github.com/iwind/TeaGo/bootstrap" + "github.com/iwind/TeaGo/lists" "github.com/iwind/TeaGo/types" "io/ioutil" + "log" + "net" "os" + "os/exec" "syscall" + "time" ) func main() { app := apps.NewAppCmd(). Version(teaconst.Version). Product(teaconst.ProductName). - Usage(teaconst.ProcessName + " [-v|start|stop|restart|quit|test]") + Usage(teaconst.ProcessName + " [-v|start|stop|restart|quit|test|daemon]") app.On("test", func() { err := nodes.NewNode().Test() @@ -25,6 +30,49 @@ func main() { _, _ = os.Stderr.WriteString(err.Error()) } }) + app.On("daemon", func() { + path := os.TempDir() + "/edge-node.sock" + isDebug := lists.ContainsString(os.Args, "debug") + isDebug = true + for { + conn, err := net.DialTimeout("unix", path, 1*time.Second) + if err != nil { + if isDebug { + log.Println("[DAEMON]starting ...") + } + + // 尝试启动 + err = func() error { + exe, err := os.Executable() + if err != nil { + return err + } + cmd := exec.Command(exe) + err = cmd.Start() + if err != nil { + return err + } + err = cmd.Wait() + if err != nil { + return err + } + return nil + }() + + if err != nil { + if isDebug { + log.Println("[DAEMON]", err) + } + time.Sleep(1 * time.Second) + } else { + time.Sleep(5 * time.Second) + } + } else { + _ = conn.Close() + time.Sleep(5 * time.Second) + } + } + }) app.On("quit", func() { pidFile := Tea.Root + "/bin/pid" data, err := ioutil.ReadFile(pidFile) diff --git a/internal/nodes/system_services.go b/internal/nodes/system_services.go index 96999cf..31df900 100644 --- a/internal/nodes/system_services.go +++ b/internal/nodes/system_services.go @@ -14,6 +14,7 @@ import ( "os" "os/exec" "runtime" + "time" ) func init() { @@ -94,6 +95,12 @@ func (this *SystemServiceManager) setupSystemd(params maps.Map) error { return err } + // 启动Service + go func() { + time.Sleep(5 * time.Second) + _ = exec.Command(systemctl, "start", teaconst.SystemdServiceName).Start() + }() + if output == "enabled" { // 检查文件路径是否变化 data, err := ioutil.ReadFile("/etc/systemd/system/" + teaconst.SystemdServiceName + ".service") diff --git a/internal/utils/service_linux.go b/internal/utils/service_linux.go index 7d43ed6..9464465 100644 --- a/internal/utils/service_linux.go +++ b/internal/utils/service_linux.go @@ -126,10 +126,10 @@ Before=shutdown.target After=network-online.target [Service] -Type=forking +Type=simple Restart=always RestartSec=1s -ExecStart=` + exePath + ` start +ExecStart=` + exePath + ` daemon ExecStop=` + exePath + ` stop ExecReload=` + exePath + ` reload