diff --git a/README.md b/README.md index cfca507..e683c9e 100644 --- a/README.md +++ b/README.md @@ -167,17 +167,23 @@ URL 里面也可放置占位符,请求时会进行简单的字符串替换。 #!/bin/sh /etc/rc.common START=99 +USE_PROCD=1 -start(){ - nohup /root/nezha-agent -i XXX -d >/dev/null 2>&1 & +start_service() { + procd_open_instance + procd_set_param command /root/nezha-agent -i xxx -p 111 -d + procd_set_param respawn + procd_close_instance } -stop(){ - # kill your pid - kill -9 `ps | grep '/root/nezha-agent' | grep -v 'grep' | awk '{print $1}'` + +stop_service() { + killall nezha-agent } -restart(){ - kill -9 `ps | grep '/root/nezha-agent' | grep -v 'grep' | awk '{print $1}'` - nohup /root/nezha-agent -i XXX -d >/dev/null 2>&1 & + +restart() { + stop + sleep 2 + start } ``` diff --git a/cmd/agent/main.go b/cmd/agent/main.go index 6484781..135533f 100644 --- a/cmd/agent/main.go +++ b/cmd/agent/main.go @@ -251,9 +251,10 @@ func doTask(task *pb.Task) { select { case <-timeout.C: result.Data = "任务执行超时\n" - pg.Dispose() close(endCh) + pg.Dispose() case <-endCh: + timeout.Stop() } }() output, err := cmd.Output() diff --git a/cmd/playground/main.go b/cmd/playground/main.go index e6c581f..388e53c 100644 --- a/cmd/playground/main.go +++ b/cmd/playground/main.go @@ -84,14 +84,15 @@ func cmdExec() { panic(err) } if utils.IsWindows() { - cmd = exec.Command("cmd", "/c", execFrom+"/cmd/playground/example.sh hello asd") + cmd = exec.Command("cmd", "/c", os.Args[1]) + // cmd = exec.Command("cmd", "/c", execFrom+"/cmd/playground/example.sh hello asd") } else { cmd = exec.Command("sh", "-c", execFrom+`/cmd/playground/example.sh hello && \ echo world!`) } pg.AddProcess(cmd) go func() { - time.Sleep(time.Second * 2) + time.Sleep(time.Second * 10) if err = pg.Dispose(); err != nil { panic(err) } diff --git a/go.mod b/go.mod index ece9295..51ea4d3 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,6 @@ require ( github.com/spf13/viper v1.7.1 github.com/stretchr/testify v1.6.1 golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43 - golang.org/x/sys v0.0.0-20201024232916-9f70ab9862d5 google.golang.org/grpc v1.33.1 google.golang.org/protobuf v1.25.0 gopkg.in/yaml.v2 v2.2.8 diff --git a/pkg/utils/proccess_group.go b/pkg/utils/proccess_group.go index 4d42f5a..c7a24ed 100644 --- a/pkg/utils/proccess_group.go +++ b/pkg/utils/proccess_group.go @@ -15,7 +15,7 @@ func NewProcessExitGroup() (ProcessExitGroup, error) { return ProcessExitGroup{}, nil } -func (g ProcessExitGroup) Dispose() error { +func (g *ProcessExitGroup) Dispose() error { for _, c := range g.cmds { if err := syscall.Kill(-c.Process.Pid, syscall.SIGKILL); err != nil { return err diff --git a/pkg/utils/proccess_group_windows.go b/pkg/utils/proccess_group_windows.go index 19ba77f..55bfe30 100644 --- a/pkg/utils/proccess_group_windows.go +++ b/pkg/utils/proccess_group_windows.go @@ -3,49 +3,28 @@ package utils import ( + "fmt" "os/exec" - "unsafe" - - "golang.org/x/sys/windows" ) -// We use this struct to retreive process handle(which is unexported) -// from os.Process using unsafe operation. -type process struct { - Pid int - Handle uintptr +type ProcessExitGroup struct { + cmds []*exec.Cmd } -type ProcessExitGroup windows.Handle - func NewProcessExitGroup() (ProcessExitGroup, error) { - handle, err := windows.CreateJobObject(nil, nil) - if err != nil { - return 0, err - } - - info := windows.JOBOBJECT_EXTENDED_LIMIT_INFORMATION{ - BasicLimitInformation: windows.JOBOBJECT_BASIC_LIMIT_INFORMATION{ - LimitFlags: windows.JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, - }, - } - if _, err := windows.SetInformationJobObject( - handle, - windows.JobObjectExtendedLimitInformation, - uintptr(unsafe.Pointer(&info)), - uint32(unsafe.Sizeof(info))); err != nil { - return 0, err - } - - return ProcessExitGroup(handle), nil + return ProcessExitGroup{}, nil } -func (g ProcessExitGroup) Dispose() error { - return windows.CloseHandle(windows.Handle(g)) +func (g *ProcessExitGroup) Dispose() error { + for _, c := range g.cmds { + if err := exec.Command("taskkill", "/F", "/T", "/PID", fmt.Sprint(c.Process.Pid)).Run(); err != nil { + return err + } + } + return nil } -func (g ProcessExitGroup) AddProcess(cmd *exec.Cmd) error { - return windows.AssignProcessToJobObject( - windows.Handle(g), - windows.Handle((*process)(unsafe.Pointer(cmd.Process)).Handle)) +func (g *ProcessExitGroup) AddProcess(cmd *exec.Cmd) error { + g.cmds = append(g.cmds, cmd) + return nil }