How stop caddy file-server?

1. Caddy version (caddy version):

v2.4.6

2. How I run Caddy:

I run Caddy via Mac Terminal

a. System environment:

macOS 12.2.1

b. Command:

./caddy_darwin_amd64 file-server --listen :2015 --root ../content/

c. Service/unit/compose file:

none

d. My complete Caddyfile or JSON config:

none

3. The problem I’m having:

I have started a file-server which runs fine. But I can not gracefully stop it.

4. Error messages and/or full log output:

failed using API to stop instance	{"error": "performing request: Post \"http://localhost:2019/stop\": dial tcp [::1]:2019: connect: connection refused"}

5. What I already tried:

The following command does not work:

./caddy_darwin_amd64 stop file-server --address 127.0.0.1:2015

The only way I found to stop the file-server is this command:

kill -9 $(lsof -ti:2015)

6. Links to relevant resources:

It seems like the admin endpoint is turned off - can you print the .config/caddy/autosave.json

@francislavoie : I had some questions about the admin endpoint myself like:

  1. If using caddy run --config /etc/caddy/Caddyfile --adapter caddyfile, will it be enabled if not configured explicitly (eg: global options block)

  2. How does the caddy run --config… behavior differ from a simple caddy run as far as the admin endpoint is concerned

  3. How does that behavior differ from other ways to invoke caddy, like the file-server in the OP?

Where can I find .config/caddy/autosave.json?
I dont find this in my user/.config folder.
PS: I dont use a caddyfile

if there’s no Info log that says “admin endpoint started”, it’s extremely likely the admin endpoint’s unavailable

Until someone more educated on this than I gets to it, try turning on debug logs and see if you get “admin endpoint started” and logs around

When you run the file-server command, Caddy remains attached to your terminal, so you should be able to hit Ctrl+C to send the SIGINT signal to Caddy to shut it down.

That command is just meant for quick one-off use, if you plan to run the server long-term, write a Caddyfile and run Caddy with caddy run, and run it as a service (so it gets started along with your system).

If you don’t specify a config, Caddy will try to load a config from your current directory if possible. And if there’s no config found, Caddy won’t start.

Caddy will enable its admin endpoint listening on localhost:2019 by default.

The docs for file-server explain it doesn’t start the admin API:

2 Likes

Thanks for your answer.
I use the file-server command in a bash script which is used by my application to start caddy.
I need to pass a port number via $1 to --listen of the file-server command.
And I need to turn off caddy via bash script when my application is closed.
Is kill -9 $(lsof -ti:${caddy_port}) the right way to shut it down?

kill -9 sends SIGKILL, which doesn’t give a chance to Caddy to gracefully shut down. Use -2 instead for SIGINT which tells Caddy to shut down gracefully.

2 Likes

Thank you!

This topic was automatically closed after 30 days. New replies are no longer allowed.