mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 12:48:14 -05:00
🐛 fix: windows 杀子进程
This commit is contained in:
parent
b5a0b0ff16
commit
52d9a4597b
22
README.md
22
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
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -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()
|
||||
|
@ -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
1
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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user