From f3fe8284e5ab3aa464fc0c2be9eb5c993e5d85d0 Mon Sep 17 00:00:00 2001 From: u9g Date: Fri, 25 Sep 2026 10:15:48 -0400 Subject: [PATCH] Add PRE_START_SCRIPT, run after setup and before the server starts (#4293) --- docs/configuration/misc-options.md | 16 ++++++++++++++++ docs/variables.md | 6 ++++++ scripts/start-finalExec | 21 +++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/docs/configuration/misc-options.md b/docs/configuration/misc-options.md index 9f0c6c98..976f8557 100644 --- a/docs/configuration/misc-options.md +++ b/docs/configuration/misc-options.md @@ -209,6 +209,22 @@ By default, the image finishes startup by exec'ing [mc-server-runner](https://gi > [!TIP] > The `SERVER_RUNNER` variable is expanded when passed to `exec` so that `mc-server-runner` can be wrapped by another executable that is mounted into the container. +## Pre-start script + +To run something of your own after the image has finished setting up the server files and just before it starts the server, set `PRE_START_SCRIPT` to the path of an executable file that you have mounted into the container. The image runs it directly (not through a shell) and waits for it to finish. It runs as the user the server runs as, from the server's working directory, with the same environment the server gets. If it exits with a non-zero status, the container exits without starting the server. + +For example, a script that waits for plugin jars that another process is still downloading: + +```yaml + environment: + PRE_START_SCRIPT: /hooks/wait-for-plugins + volumes: + - ./hooks:/hooks:ro +``` + +> [!WARNING] +> The image does not guarantee that any particular command is available to the script, and it may change between image versions. Keeping the script working is up to you. + ## Enable Flare Flags To enable the JVM flags required to fully support the [Flare profiling suite](https://blog.airplane.gg/flare), set the following variable: diff --git a/docs/variables.md b/docs/variables.md index 57dc83b6..7f49ecce 100644 --- a/docs/variables.md +++ b/docs/variables.md @@ -288,6 +288,12 @@ alternatively, you can mount: /etc/localtime:/etc/localtime:ro false ⬜️ + + PRE_START_SCRIPT + Path to an executable run after setup and just before the server starts. The server is not started if it exits non-zero. See Pre-start script + + ⬜️ + USE_FLARE_FLAGS To enable the JVM flags required to fully support the Flare profiling suite. diff --git a/scripts/start-finalExec b/scripts/start-finalExec index c59c33b0..6d1c4352 100755 --- a/scripts/start-finalExec +++ b/scripts/start-finalExec @@ -4,10 +4,27 @@ : "${SETUP_ONLY:=false}" : "${CUSTOM_JAR_EXEC:=}" : "${SERVER_RUNNER:=mc-server-runner}" +: "${PRE_START_SCRIPT:=}" # shellcheck source=start-utils . "$(dirname "$0")/start-utils" isDebugging && set -x + +function runPreStartScript() { + if [[ ! $PRE_START_SCRIPT ]]; then + return + fi + if [[ ! -x $PRE_START_SCRIPT ]]; then + logError "PRE_START_SCRIPT $PRE_START_SCRIPT is not an executable file" + exit 1 + fi + log "Running pre-start script $PRE_START_SCRIPT..." + if ! "$PRE_START_SCRIPT"; then + logError "Pre-start script $PRE_START_SCRIPT failed, so the server will not be started" + exit 1 + fi +} + baseDataDir=/data tmpServerIconPath=/tmp/icon.img serverIconPath=${baseDataDir}/server-icon.png @@ -505,6 +522,7 @@ if [[ ${TYPE} == "CURSEFORGE" && "${SERVER}" ]]; then exit 1 fi log "Starting CurseForge server in ${FTB_DIR}..." + runPreStartScript if isTrue "${DEBUG_EXEC}"; then set -x fi @@ -569,6 +587,7 @@ fi JVM_ARGS=${JVM_ARGS//$'\n'/} sed -i "s~JAVA_ARGS=.*~JAVA_ARGS=\"${JVM_ARGS}\"~" "${FTB_DIR}/variables.txt" fi + runPreStartScript exec ${SERVER_RUNNER} "${mcServerRunnerArgs[@]}" -- "${finalArgs[@]}" elif [[ $SERVER =~ run.sh ]]; then log "Using Forge supplied run.sh script..." @@ -577,6 +596,7 @@ elif [[ $SERVER =~ run.sh ]]; then echo "SETUP_ONLY: bash ${SERVER}" exit fi + runPreStartScript if isTrue "${DEBUG_EXEC}"; then set -x fi @@ -614,6 +634,7 @@ else exit fi + runPreStartScript if isTrue "${DEBUG_EXEC}"; then set -x fi