Have Caddy run a command when a site is accessed?

This is more of a general request for information than a specific problem I’m having, but is there a way for Caddy itself to run “touch /tmp/jellyfin” when “jellyfin.domain.com” is accessed on the server?

Basically my thought is that I could have a systemd script on my server that tries to read /tmp/jellyfin and if it’s present it will then start the docker container.

However if the /tmp/jellyfin file hasn’t been touched in ages, I’d have that systemd script delete /tmp/jellyfin, and then if it fails to see /tmp/jellyfin (because of a lack of access) it’ll just shut down the application.

I’m trying to reduce wear/tear on my spinning disk drives which are constantly spinning up and just shutting down the container seems to me to be the most elegant solution. I could probably do this all with some serverside CGI scripting or something but I’m curious if Caddy is set up to do something like that.

If not I’ll just have Caddy forward all requests from “jellyfin.domain.com” to jellyfin.domain.com/start and then have a cgi script that does what I want and then have it forward back to jellyfin.domain.com/jf/* or something like that once it’s running?

Just brainstorming here, but I’d like to do this for several of my services that I don’t need running 24/7 and are only accessed a few times a week at most.

It seems to be like an overkill optimization but it’s something I’d like to learn how to do.

You could have a script tailing Caddy’s log and act on that.

I would, but what I want to end up with is a scenario where I get an Auto refreshing page that says “service not loaded, loading your service now”

And then starting the docker container.

I’m not sure how to dynamically change what’s served from anything other than the webserver… I’d love tips or ideas though.

Maybe this plugin can be useful for your use case:

yeah, I’d be a little careful with letting the public web path directly run commands.

you can do it with plugins, but for this use case I’d probably put a tiny local “wake” service behind caddy instead. caddy sends jellyfin.domain.com there first. that service checks whether the container is running; if not, it starts it and returns the little auto-refresh “starting up” page. once jellyfin is healthy, it can redirect or proxy through to the real service.

that keeps the command-running permission in one small systemd-managed thing instead of in the web server config.

if you only need the idle/shutdown signal, tailing caddy access logs is simpler. the loading page is the part that makes me reach for a small controller service.