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"}
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:
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.