Skip to content

Commit

Permalink
core: Chdir to executable location on Windows (#5115)
Browse files Browse the repository at this point in the history
Since all Windows services are run from the Windows system directory,
make it easier for users by switching to our program directory right
after the start.
  • Loading branch information
TobiX committed Oct 4, 2022
1 parent c28cd29 commit 253d97c
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions service_windows.go
Expand Up @@ -15,6 +15,9 @@
package caddy

import (
"os"
"path/filepath"

"github.com/caddyserver/caddy/v2/notify"
"golang.org/x/sys/windows/svc"
)
Expand All @@ -24,6 +27,14 @@ func init() {
if err != nil || !isService {
return
}

// Windows services always start in the system32 directory, try to
// switch into the directory where the caddy executable is.
execPath, err := os.Executable()
if err == nil {
_ = os.Chdir(filepath.Dir(execPath))
}

go func() {
_ = svc.Run("", runner{})
}()
Expand Down

0 comments on commit 253d97c

Please sign in to comment.