forked from ShinyTrinkets/overseer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement Stop signal across platforms
- Loading branch information
pingqixing
committed
Dec 27, 2019
1 parent
1f67a51
commit 6361fac
Showing
4 changed files
with
98 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// +build !windows | ||
|
||
package signals | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
) | ||
|
||
func SendSignal(pid int, sig syscall.Signal) error { | ||
proc, err := os.FindProcess(-pid) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = proc.Signal(sig) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Kill(process *os.Process, sig os.Signal, sigChildren bool) error { | ||
localSig := sig.(syscall.Signal) | ||
pid := process.Pid | ||
if sigChildren { | ||
pid = -pid | ||
} | ||
return syscall.Kill(pid, localSig) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// +build windows | ||
|
||
package signals | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"syscall" | ||
) | ||
|
||
func SendSignal(pid int, sig syscall.Signal) error { | ||
proc, err := os.FindProcess(-pid) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = proc.Signal(sig) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func Kill(process *os.Process, sig os.Signal, sigChildren bool) error { | ||
// Signal command can't kill children processes, call taskkill command to kill them | ||
cmd := exec.Command("taskkill", "/F", "/T", "/PID", fmt.Sprintf("%d", process.Pid)) | ||
err := cmd.Start() | ||
if err == nil { | ||
return cmd.Wait() | ||
} | ||
// if fail to find taskkill, fallback to normal signal | ||
return process.Signal(sig) | ||
} |