Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to program directory when running as Windows service #5115

Merged
merged 1 commit into from Oct 4, 2022

Conversation

TobiX
Copy link
Contributor

@TobiX TobiX commented Oct 3, 2022

As noted in #5106, when installing Caddy as a native Windows service (For reference, the PowerShell command for that is New-Service -Name caddy -BinaryPathName "C:\caddy\caddy.exe run"), caddy starts up in C:\WINDOWS\SYSTEM32 - I think this is a bad idea for multiple reasons:

  • You obviously cannot put an "adjacent" Caddyfile into system32 (well, you technically can, but that sound like a bad idea all around) and as an alternative, you have to run caddy with a much longer command line (Like New-Service -Name caddy -BinaryPathName "C:\caddy\caddy.exe run --config C:\caddy\Caddyfile"
  • If somebody tries to use relative paths in the Caddyfile, those will be relative to the system32 directory, which will lead to hard-to-debug problems...

This is a "prototype" of a simple fix: When we are running as a Windows service, try to chdir into the caddy program directory - It works for me[tm] in a Windows 10 VM...

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.
Copy link
Member

@mholt mholt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I didn't know that, so I agree this is probably a better default.

Ignoring the error here is probably fine? I'll merge it, but I wonder if we should at least log a warning.

@mholt mholt merged commit 253d97c into caddyserver:master Oct 4, 2022
@francislavoie francislavoie added this to the v2.6.2 milestone Oct 4, 2022
@francislavoie francislavoie added the bug 🐞 Something isn't working label Oct 4, 2022
@TobiX
Copy link
Contributor Author

TobiX commented Oct 4, 2022

@mholt Unfortunately, there is nowhere to log TO when running as a windows service, since STDOUT & STDERR are discarded... IF somebody implements Windows event logging (https://pkg.go.dev/golang.org/x/sys/windows/svc/eventlog), that should become the default for Windows services, so errors can be logged... somewhere 😸

@TobiX TobiX deleted the windows-service-workdir branch October 4, 2022 17:28
@mholt
Copy link
Member

mholt commented Oct 4, 2022

Good to know, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants