Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
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
Toby Allen committed
Jul 20, 2019 at 17:44 UTC
b855e661700fa8f9176f29c3c5b1cebd4cc09351
3 files changed
+24
-4
cmd/commands.go
+3
-3
@@ -27,7 +27,7 @@ import (
27
"net/http"
28
"os"
29
"os/exec"
30
- "path/filepath"
30
+
31
"strings"
32
33
"github.com/caddyserver/caddy/v2"
@@ -203,14 +203,14 @@ func cmdStop() (int, error) {
203
if err != nil {
204
return caddy.ExitCodeFailedStartup, fmt.Errorf("listing processes: %v", err)
205
}
206
- thisProcName := filepath.Base(os.Args[0])
206
+ thisProcName := getProcessName()
207
var found bool
208
for _, p := range processList {
209
// the process we're looking for should have the same name but different PID
210
if p.Executable() == thisProcName && p.Pid() != os.Getpid() {
211
found = true
212
fmt.Printf("pid=%d\n", p.Pid())
213
- fmt.Printf("Graceful stop...")
213
+
214
if err := gracefullyStopProcess(p.Pid()); err != nil {
215
return caddy.ExitCodeFailedStartup, err
216
}
cmd/proc_posix.go
+5
@@ -22,9 +22,14 @@ import (
22
)
23
24
func gracefullyStopProcess(pid int) error {
25
+ fmt.Printf("Graceful stop...")
26
err := syscall.Kill(pid, syscall.SIGINT)
27
if err != nil {
28
return fmt.Errorf("kill: %v", err)
29
}
30
return nil
31
}
32
+
33
+func getProcessName() string {
34
+ return filepath.Base(os.Args[0])
35
+}
cmd/proc_windows.go
+16
-1
@@ -16,14 +16,29 @@ package caddycmd
16
17
import (
18
"fmt"
19
+ "os"
20
"os/exec"
21
+ "path/filepath"
22
"strconv"
23
)
24
25
func gracefullyStopProcess(pid int) error {
24
- cmd := exec.Command("taskkill", "/pid", strconv.Itoa(pid))
26
+ fmt.Printf("Forceful Stop...")
27
+ // process on windows will not stop unless forced with /f
28
+ cmd := exec.Command("taskkill", "/pid", strconv.Itoa(pid), "/f")
29
if err := cmd.Run(); err != nil {
30
return fmt.Errorf("taskkill: %v", err)
31
}
32
return nil
33
}
34
+
35
+// On Windows the app name passed in os.Args[0] will match how
36
+// caddy was started eg will match caddy or caddy.exe.
37
+// So return appname with .exe for consistency
38
+func getProcessName() string {
39
+ base := filepath.Base(os.Args[0])
40
+ if filepath.Ext(base) == "" {
41
+ return base + ".exe"
42
+ }
43
+ return base
44
+}