mirror of
https://github.com/nezhahq/nezha.git
synced 2025-01-22 20:58: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
|
#!/bin/sh /etc/rc.common
|
||||||
|
|
||||||
START=99
|
START=99
|
||||||
|
USE_PROCD=1
|
||||||
|
|
||||||
start(){
|
start_service() {
|
||||||
nohup /root/nezha-agent -i XXX -d >/dev/null 2>&1 &
|
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
|
stop_service() {
|
||||||
kill -9 `ps | grep '/root/nezha-agent' | grep -v 'grep' | awk '{print $1}'`
|
killall nezha-agent
|
||||||
}
|
}
|
||||||
restart(){
|
|
||||||
kill -9 `ps | grep '/root/nezha-agent' | grep -v 'grep' | awk '{print $1}'`
|
restart() {
|
||||||
nohup /root/nezha-agent -i XXX -d >/dev/null 2>&1 &
|
stop
|
||||||
|
sleep 2
|
||||||
|
start
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -251,9 +251,10 @@ func doTask(task *pb.Task) {
|
|||||||
select {
|
select {
|
||||||
case <-timeout.C:
|
case <-timeout.C:
|
||||||
result.Data = "任务执行超时\n"
|
result.Data = "任务执行超时\n"
|
||||||
pg.Dispose()
|
|
||||||
close(endCh)
|
close(endCh)
|
||||||
|
pg.Dispose()
|
||||||
case <-endCh:
|
case <-endCh:
|
||||||
|
timeout.Stop()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
output, err := cmd.Output()
|
output, err := cmd.Output()
|
||||||
|
@ -84,14 +84,15 @@ func cmdExec() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if utils.IsWindows() {
|
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 {
|
} else {
|
||||||
cmd = exec.Command("sh", "-c", execFrom+`/cmd/playground/example.sh hello && \
|
cmd = exec.Command("sh", "-c", execFrom+`/cmd/playground/example.sh hello && \
|
||||||
echo world!`)
|
echo world!`)
|
||||||
}
|
}
|
||||||
pg.AddProcess(cmd)
|
pg.AddProcess(cmd)
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(time.Second * 2)
|
time.Sleep(time.Second * 10)
|
||||||
if err = pg.Dispose(); err != nil {
|
if err = pg.Dispose(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
1
go.mod
1
go.mod
@ -22,7 +22,6 @@ require (
|
|||||||
github.com/spf13/viper v1.7.1
|
github.com/spf13/viper v1.7.1
|
||||||
github.com/stretchr/testify v1.6.1
|
github.com/stretchr/testify v1.6.1
|
||||||
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43
|
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/grpc v1.33.1
|
||||||
google.golang.org/protobuf v1.25.0
|
google.golang.org/protobuf v1.25.0
|
||||||
gopkg.in/yaml.v2 v2.2.8
|
gopkg.in/yaml.v2 v2.2.8
|
||||||
|
@ -15,7 +15,7 @@ func NewProcessExitGroup() (ProcessExitGroup, error) {
|
|||||||
return ProcessExitGroup{}, nil
|
return ProcessExitGroup{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g ProcessExitGroup) Dispose() error {
|
func (g *ProcessExitGroup) Dispose() error {
|
||||||
for _, c := range g.cmds {
|
for _, c := range g.cmds {
|
||||||
if err := syscall.Kill(-c.Process.Pid, syscall.SIGKILL); err != nil {
|
if err := syscall.Kill(-c.Process.Pid, syscall.SIGKILL); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -3,49 +3,28 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"golang.org/x/sys/windows"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// We use this struct to retreive process handle(which is unexported)
|
type ProcessExitGroup struct {
|
||||||
// from os.Process using unsafe operation.
|
cmds []*exec.Cmd
|
||||||
type process struct {
|
|
||||||
Pid int
|
|
||||||
Handle uintptr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProcessExitGroup windows.Handle
|
|
||||||
|
|
||||||
func NewProcessExitGroup() (ProcessExitGroup, error) {
|
func NewProcessExitGroup() (ProcessExitGroup, error) {
|
||||||
handle, err := windows.CreateJobObject(nil, nil)
|
return ProcessExitGroup{}, 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g ProcessExitGroup) Dispose() error {
|
func (g *ProcessExitGroup) Dispose() error {
|
||||||
return windows.CloseHandle(windows.Handle(g))
|
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 {
|
func (g *ProcessExitGroup) AddProcess(cmd *exec.Cmd) error {
|
||||||
return windows.AssignProcessToJobObject(
|
g.cmds = append(g.cmds, cmd)
|
||||||
windows.Handle(g),
|
return nil
|
||||||
windows.Handle((*process)(unsafe.Pointer(cmd.Process)).Handle))
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user