-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
升级前后的守护进程处理;优化寻找进程方法;Fix #15:Win覆盖文件名造成的守护进程判断异常
- Loading branch information
1 parent
1f59eff
commit 70b79a0
Showing
6 changed files
with
146 additions
and
76 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
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