Created
September 30, 2026 04:08
-
-
Save SomeBottle/2bcb797414a5045bc5099eb769851617 to your computer and use it in GitHub Desktop.
Minecraft 服务器启动脚本(自动使用代理环境变量)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # | |
| # Minecraft 服务器启动脚本 | |
| # 用法: | |
| # HTTP_PROXY=http://127.0.0.1:7890 \ | |
| # HTTPS_PROXY=http://127.0.0.1:7890 \ | |
| # ./start.sh | |
| # | |
| set -euo pipefail | |
| # ---------------- 配置区 ---------------- | |
| SERVER_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | |
| JAR="purpur-26.2-2633.jar" | |
| MIN_MEM="1G" | |
| MAX_MEM="1G" | |
| AUTO_RESTART=false | |
| RESTART_DELAY=5 | |
| # ---------------------------------------- | |
| cd "$SERVER_DIR" | |
| # ---- 环境检查 ---- | |
| if ! command -v java >/dev/null 2>&1; then | |
| echo "错误:未找到 java,请先安装 JDK 17+。" >&2 | |
| exit 1 | |
| fi | |
| if [[ ! -f "$JAR" ]]; then | |
| echo "错误:当前目录下找不到 $JAR。" >&2 | |
| exit 1 | |
| fi | |
| if [[ ! -f eula.txt ]]; then | |
| echo "提示:首次启动后需编辑 eula.txt,把 eula=false 改为 true。" | |
| fi | |
| # ---- JVM 参数 ---- | |
| JVM_ARGS=( | |
| -Xms"$MIN_MEM" | |
| -Xmx"$MAX_MEM" | |
| ) | |
| # ---- 解析代理环境变量 ---- | |
| # 参数: | |
| # $1 = Java 代理类型:http / https | |
| # $2 = 代理 URL | |
| # | |
| # 示例: | |
| # parse_proxy http "http://127.0.0.1:7890" | |
| # | |
| parse_proxy() { | |
| local type="$1" | |
| local proxy="$2" | |
| [[ -z "$proxy" ]] && return | |
| # 去掉协议头 | |
| local address="${proxy#*://}" | |
| # 去掉可能存在的用户名/密码 | |
| # user:pass@127.0.0.1:7890 -> 127.0.0.1:7890 | |
| address="${address##*@}" | |
| local host | |
| local port | |
| # IPv6: [::1]:7890 | |
| if [[ "$address" =~ ^\[([^]]+)\]:([0-9]+)$ ]]; then | |
| host="${BASH_REMATCH[1]}" | |
| port="${BASH_REMATCH[2]}" | |
| # IPv4 / hostname: 127.0.0.1:7890 | |
| elif [[ "$address" =~ ^([^:]+):([0-9]+)$ ]]; then | |
| host="${BASH_REMATCH[1]}" | |
| port="${BASH_REMATCH[2]}" | |
| else | |
| echo "警告:无法解析 ${type^^}_PROXY: $proxy" >&2 | |
| return | |
| fi | |
| JVM_ARGS+=( | |
| "-D${type}.proxyHost=${host}" | |
| "-D${type}.proxyPort=${port}" | |
| ) | |
| echo "已配置 ${type^^} 代理: ${host}:${port}" | |
| } | |
| # 优先使用大写环境变量,同时兼容小写 | |
| HTTP_PROXY_VALUE="${HTTP_PROXY:-${http_proxy:-}}" | |
| HTTPS_PROXY_VALUE="${HTTPS_PROXY:-${https_proxy:-}}" | |
| parse_proxy "http" "$HTTP_PROXY_VALUE" | |
| parse_proxy "https" "$HTTPS_PROXY_VALUE" | |
| # ---- 启动服务器 ---- | |
| start_server() { | |
| echo "[$(date '+%F %T')] 正在启动服务器..." | |
| java "${JVM_ARGS[@]}" -jar "$JAR" --nogui | |
| } | |
| # ---- 启动 ---- | |
| if [[ "$AUTO_RESTART" == true ]]; then | |
| while true; do | |
| start_server || echo "[$(date '+%F %T')] 服务器退出,状态码 $?" | |
| echo "[$(date '+%F %T')] ${RESTART_DELAY} 秒后自动重启(Ctrl+C 终止)..." | |
| sleep "$RESTART_DELAY" | |
| done | |
| else | |
| start_server | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment