mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
Force quit on Windows with taskkill /f (#2670)
* Force quit /f on windows, also check for processname '.exe' on windows. * Remove unneeded spaces * fix tabs * go fmt tabs * Return consistent appname which always includes .exe * Change func name
This commit is contained in:
parent
0d3f99e85a
commit
b855e66170
3 changed files with 24 additions and 4 deletions
|
@ -27,7 +27,7 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/caddy/v2"
|
||||
|
@ -203,14 +203,14 @@ func cmdStop() (int, error) {
|
|||
if err != nil {
|
||||
return caddy.ExitCodeFailedStartup, fmt.Errorf("listing processes: %v", err)
|
||||
}
|
||||
thisProcName := filepath.Base(os.Args[0])
|
||||
thisProcName := getProcessName()
|
||||
var found bool
|
||||
for _, p := range processList {
|
||||
// the process we're looking for should have the same name but different PID
|
||||
if p.Executable() == thisProcName && p.Pid() != os.Getpid() {
|
||||
found = true
|
||||
fmt.Printf("pid=%d\n", p.Pid())
|
||||
fmt.Printf("Graceful stop...")
|
||||
|
||||
if err := gracefullyStopProcess(p.Pid()); err != nil {
|
||||
return caddy.ExitCodeFailedStartup, err
|
||||
}
|
||||
|
|
|
@ -22,9 +22,14 @@ import (
|
|||
)
|
||||
|
||||
func gracefullyStopProcess(pid int) error {
|
||||
fmt.Printf("Graceful stop...")
|
||||
err := syscall.Kill(pid, syscall.SIGINT)
|
||||
if err != nil {
|
||||
return fmt.Errorf("kill: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func getProcessName() string {
|
||||
return filepath.Base(os.Args[0])
|
||||
}
|
||||
|
|
|
@ -16,14 +16,29 @@ package caddycmd
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func gracefullyStopProcess(pid int) error {
|
||||
cmd := exec.Command("taskkill", "/pid", strconv.Itoa(pid))
|
||||
fmt.Printf("Forceful Stop...")
|
||||
// process on windows will not stop unless forced with /f
|
||||
cmd := exec.Command("taskkill", "/pid", strconv.Itoa(pid), "/f")
|
||||
if err := cmd.Run(); err != nil {
|
||||
return fmt.Errorf("taskkill: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// On Windows the app name passed in os.Args[0] will match how
|
||||
// caddy was started eg will match caddy or caddy.exe.
|
||||
// So return appname with .exe for consistency
|
||||
func getProcessName() string {
|
||||
base := filepath.Base(os.Args[0])
|
||||
if filepath.Ext(base) == "" {
|
||||
return base + ".exe"
|
||||
}
|
||||
return base
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue