File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package cmd
2+
3+ import (
4+ "errors"
5+ "fmt"
6+ "os"
7+ "strconv"
8+ "syscall"
9+
10+ "github.com/spf13/cobra"
11+ )
12+
13+ var killallCmd = & cobra.Command {
14+ Use : "killall" ,
15+ Short : "Stop all the running servers" ,
16+ Run : func (cmd * cobra.Command , args []string ) {
17+ servers := getServers ()
18+ killed := 0
19+
20+ for _ , server := range servers {
21+ pid , err := strconv .Atoi (server )
22+ if err != nil {
23+ fmt .Printf ("Invalid PID %q: %v\n " , server , err )
24+ continue
25+ }
26+
27+ process , err := os .FindProcess (pid )
28+ if err != nil {
29+ continue
30+ }
31+
32+ if err := process .Signal (syscall .SIGTERM ); err != nil {
33+ if errors .Is (err , os .ErrProcessDone ) {
34+ removeStalePidFile (pid )
35+ }
36+ continue
37+ }
38+
39+ killed ++
40+ }
41+
42+ fmt .Println ("Total servers stopped:" , killed )
43+ },
44+ }
45+
46+ func removeStalePidFile (pid int ) {
47+ userHomeDir , err := os .UserHomeDir ()
48+ if err != nil {
49+ return
50+ }
51+ _ = os .Remove (fmt .Sprintf ("%s/.uws.%d" , userHomeDir , pid ))
52+ }
Original file line number Diff line number Diff line change @@ -84,6 +84,7 @@ func Execute() {
8484 rootCmd .AddCommand (
8585 versionCmd ,
8686 listCmd ,
87+ killallCmd ,
8788 )
8889
8990 rootCmd .PersistentFlags ().BoolP ("daemon" , "d" , false ,
You can’t perform that action at this time.
0 commit comments