summaryrefslogtreecommitdiff
path: root/service_windows.go
diff options
context:
space:
mode:
authorTobias Gruetzmacher <tobias-git@23.gs>2022-10-04 19:04:02 +0200
committerGitHub <noreply@github.com>2022-10-04 11:04:02 -0600
commit253d97c93dce9a3365e67d0360b2e47dd165578e (patch)
treec6c8788b91b838451a76efe8c2b0270ff83bccd8 /service_windows.go
parentc28cd29fe7ffc4b7f7bebb16fe721f25abab2446 (diff)
core: Chdir to executable location on Windows (#5115)
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.
Diffstat (limited to 'service_windows.go')
-rw-r--r--service_windows.go11
1 files changed, 11 insertions, 0 deletions
diff --git a/service_windows.go b/service_windows.go
index 6db2992..357c9ac 100644
--- a/service_windows.go
+++ b/service_windows.go
@@ -15,6 +15,9 @@
package caddy
import (
+ "os"
+ "path/filepath"
+
"github.com/caddyserver/caddy/v2/notify"
"golang.org/x/sys/windows/svc"
)
@@ -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{})
}()