Skip to content

Commit

Permalink
Auto-start, stop, and add service description (#2)
Browse files Browse the repository at this point in the history
Automatically start the service upon installation
* Set startup type to Automatic

Automatically stop the service prior to removal

Add service description to improve discoverability within services.msc
  • Loading branch information
wheatevo authored Jan 20, 2021
1 parent 285c471 commit 049d80e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Install the service (as Administrator):
.\wslroutesvc.exe install
```

After installing the service, start it and set it to automatic within services.msc or enable/start it on the command line.
This command will start the service and set it to automatic startup.
13 changes: 11 additions & 2 deletions install.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"

"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/eventlog"
"golang.org/x/sys/windows/svc/mgr"
)
Expand Down Expand Up @@ -37,7 +38,7 @@ func exePath() (string, error) {
return "", err
}

func installService(name, desc string) error {
func installService(name, display, desc string) error {
exepath, err := exePath()
if err != nil {
return err
Expand All @@ -52,7 +53,7 @@ func installService(name, desc string) error {
s.Close()
return fmt.Errorf("service %s already exists", name)
}
s, err = m.CreateService(name, exepath, mgr.Config{DisplayName: desc}, "is", "auto-started")
s, err = m.CreateService(name, exepath, mgr.Config{StartType: mgr.StartAutomatic, DisplayName: display, Description: desc}, "is", "auto-started")
if err != nil {
return err
}
Expand All @@ -62,6 +63,10 @@ func installService(name, desc string) error {
s.Delete()
return fmt.Errorf("SetupEventLogSource() failed: %s", err)
}

// start the service after setting it to automatic
startService(name)

return nil
}

Expand All @@ -76,6 +81,10 @@ func removeService(name string) error {
return fmt.Errorf("service %s is not installed", name)
}
defer s.Close()

// stop the service prior to deletion
controlService(name, svc.Stop, svc.Stopped)

err = s.Delete()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {
runService(svcName, true)
return
case "install":
err = installService(svcName, "WSL VPN Routing Conflict Service")
err = installService(svcName, "WSL VPN Routing Conflict Service", "Automatically removes duplicate VPN network routes that conflict with internal WSL routing.")
case "remove":
err = removeService(svcName)
case "start":
Expand Down

0 comments on commit 049d80e

Please sign in to comment.