-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Karmenzind/dev
Dev
- Loading branch information
Showing
7 changed files
with
149 additions
and
80 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
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,51 @@ | ||
package proc | ||
|
||
import ( | ||
"os/exec" | ||
"runtime" | ||
"strconv" | ||
"syscall" | ||
|
||
"github.com/shirou/gopsutil/v3/process" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// not for Win | ||
func KillProcess(p *process.Process) (err error) { | ||
if runtime.GOOS != "windows" { | ||
errSig := p.SendSignal(syscall.SIGINT) | ||
if errSig == nil { | ||
if yes, errCheck := p.IsRunning(); errCheck == nil && !yes { | ||
zap.S().Infof("Stopped process %v with SIGINT.", p.Pid) | ||
return | ||
} | ||
} else { | ||
zap.S().Warnf("Failed to stop PID %v with syscall.SIGINT: %s", p.Pid, errSig) | ||
} | ||
} | ||
return SysKillPID(p.Pid) | ||
} | ||
|
||
func GetKillCMD(pid int32) *exec.Cmd { | ||
pidStr := strconv.Itoa(int(pid)) | ||
var cmd *exec.Cmd | ||
switch runtime.GOOS { | ||
case "windows": | ||
cmd = exec.Command("taskkill", "/F", "/T", "/PID", pidStr) | ||
// cmd = exec.Command("taskkill", "/im", "kd", "/T", "/F") | ||
case "linux": | ||
cmd = exec.Command("kill", "-9", pidStr) | ||
// cmd = exec.Command("killall", "kd") | ||
} | ||
return cmd | ||
} | ||
|
||
func SysKillPID(pid int32) (err error) { | ||
cmd := GetKillCMD(pid) | ||
output, err := cmd.Output() | ||
zap.S().Infof("Executed '%s'. Output %s", cmd, output) | ||
if err != nil { | ||
zap.S().Warnf("Failed to kill %v with system command. Output: `%s` Error: `%s`", pid, output, err) | ||
} | ||
return | ||
} |
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