Caddy v2 module for running one-off commands
Caddy v2 module for running one-off commands.
xcaddy build \
--with github.com/abiosoft/caddy-exec
Commands can be configured to be triggered globally during startup/shutdown or by via a route.
They can also be configured to run in the background or foreground and to be terminated after a timeout.
:warning: startup commands running on foreground will prevent Caddy from starting if they exit with an error.
exec [<matcher>] [<command> [<args...>]] {
command <command> [<args...>]
args <args...>
directory <directory>
timeout <timeout>
log <log output module>
err_log <log output module>
foreground
pass_thru
startup
shutdown
}
10s
. A timeout of 0
runs indefinitely.stderr
.log
(standard output log).exec
can run at start via the global directive.
{
exec hugo generate --destination=/home/user/site/public {
timeout 0 # don't timeout
}
}
exec
can be the last action of a route block.
route /update {
... # other directives e.g. for authentication
exec git pull origin master {
log file /var/logs/hugo.log
}
}
As a top level app for startup
and shutdown
commands.
{
"apps": {
"http": { ... },
// app configuration
"exec": {
// list of commands
"commands": [
// command configuration
{
// command to execute
"command": "hugo",
// [optional] command arguments
"args": [
"generate",
"--destination=/home/user/site/public"
],
// when to run the command, can include 'startup' or 'shutdown'
"at": ["startup"],
// [optional] directory to run the command from. Default is the current directory.
"directory": "",
// [optional] if the command should run on the foreground. Default is false.
"foreground": false,
// [optional] if the middleware should respond directly or pass the request on to the next handler in the route. Default is false.
"pass_thru": false,
// [optional] timeout to terminate the command's process. Default is 10s.
"timeout": "10s",
// [optional] log output module config for standard output. Default is `stderr` module.
"log": {
"output": "file",
"filename": "/var/logs/hugo.log"
},
// [optional] log output module config for standard error. Default is the value of `log`.
"err_log": {
"output": "stderr"
}
}
]
}
}
}
As an handler within a route.
{
...
"routes": [
{
"handle": [
// exec configuration for an endpoint route
{
// required to inform caddy the handler is `exec`
"handler": "exec",
// command to execute
"command": "git",
// command arguments it's also possible to use
// caddy variables like {http.request.uuid}
"args": ["pull", "origin", "master", "# {http.request.uuid}"],
// [optional] directory to run the command from. Default is the current directory.
"directory": "/home/user/site/public",
// [optional] if the command should run on the foreground. Default is false.
"foreground": true,
// [optional] if the middleware should respond directly or pass the request on to the next handler in the route. Default is false.
"pass_thru": true,
// [optional] timeout to terminate the command's process. Default is 10s.
"timeout": "5s",
// [optional] log output module config for standard output. Default is `stderr` module.
"log": {
"output": "file",
"filename": "/var/logs/hugo.log"
},
// [optional] log output module config for standard error. Default is the value of `log`.
"err_log": {
"output": "stderr"
}
}
],
"match": [
{
"path": ["/update"]
}
]
}
]
}
Caddy supports dynamic zero-downtime configuration reloads and it is possible to modify exec
's configurations at runtime.
exec
intelligently determines when Caddy is starting and shutting down. i.e. startup and shutdown commands do not get triggered during configuration reload, only during Caddy's actual startup and shutdown.
Apache 2