mirror of
https://github.com/shirou/gopsutil.git
synced 2025-04-26 13:48:59 +08:00
Merge pull request #459 from shirou/feature/implements_process_kill_on_windows
[process]windows: implements process.Kill using os/exec
This commit is contained in:
commit
27389f01ec
@ -3,6 +3,7 @@ package process
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"reflect"
|
||||
"runtime"
|
||||
@ -418,5 +419,24 @@ func Test_OpenFiles(t *testing.T) {
|
||||
for _, vv := range v {
|
||||
assert.NotEqual(t, "", vv.Path)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_Kill(t *testing.T) {
|
||||
var cmd *exec.Cmd
|
||||
if runtime.GOOS == "windows" {
|
||||
cmd = exec.Command("choice", "/C", "YN", "/D", "Y", "/t", "3")
|
||||
} else {
|
||||
cmd = exec.Command("sleep", "3")
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
assert.NotNil(t, cmd.Run())
|
||||
wg.Done()
|
||||
}()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
p, err := NewProcess(int32(cmd.Process.Pid))
|
||||
assert.Nil(t, err)
|
||||
assert.Nil(t, p.Kill())
|
||||
wg.Wait()
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ package process
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
@ -407,7 +408,8 @@ func (p *Process) Terminate() error {
|
||||
}
|
||||
|
||||
func (p *Process) Kill() error {
|
||||
return common.ErrNotImplementedError
|
||||
process := os.Process{Pid: int(p.Pid)}
|
||||
return process.Kill()
|
||||
}
|
||||
|
||||
func getFromSnapProcess(pid int32) (int32, int32, string, error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user