🐛 fix: windows 杀子进程

This commit is contained in:
naiba 2021-01-29 14:29:31 +08:00
parent b5a0b0ff16
commit 52d9a4597b
6 changed files with 34 additions and 48 deletions

View File

@ -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
}
```

View File

@ -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()

View File

@ -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)
}

1
go.mod
View File

@ -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

View File

@ -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

View File

@ -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
}