diff --git a/.gitattributes b/.gitattributes index 39a823d..14871aa 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,3 +1,5 @@ # Shell 脚本在仓库内统一 LF,避免 Linux 上 bash: pipefail: invalid option name(CRLF) *.sh text eol=lf deploy/** text eol=lf +# .env 模板统一 LF,避免 Linux PM2 source 报 $'\r': command not found +**/.env.example text eol=lf diff --git a/deploy/setup_env.ps1 b/deploy/setup_env.ps1 index 6fd6c34..6f611cb 100644 --- a/deploy/setup_env.ps1 +++ b/deploy/setup_env.ps1 @@ -62,6 +62,14 @@ function Should-Include([string]$Key, [string[]]$Selected) { return $Selected -contains $Key } +# 复制 .env 时统一为 LF,避免上传到 Linux 后 PM2 source 报 $'\r': command not found +function Copy-EnvFileLf([string]$Src, [string]$Dst) { + $raw = [System.IO.File]::ReadAllText($Src) + $lf = ($raw -replace "`r`n", "`n") -replace "`r", "`n" + $utf8NoBom = New-Object System.Text.UTF8Encoding $false + [System.IO.File]::WriteAllText($Dst, $lf, $utf8NoBom) +} + function Setup-MonitorProject([hashtable]$Proj) { $projPath = Join-Path $RepoRoot $Proj.Dir if (-not (Test-Path $projPath)) { @@ -94,8 +102,8 @@ function Setup-MonitorProject([hashtable]$Proj) { $envExample = Join-Path $projPath ".env.example" $envFile = Join-Path $projPath ".env" if ((Test-Path $envExample) -and -not (Test-Path $envFile)) { - Copy-Item $envExample $envFile - Write-Host " 已复制 .env.example -> .env" -ForegroundColor Green + Copy-EnvFileLf $envExample $envFile + Write-Host " 已复制 .env.example -> .env (LF)" -ForegroundColor Green } elseif (Test-Path $envFile) { Write-Host " 保留已有 .env" -ForegroundColor DarkGray } else { @@ -147,8 +155,8 @@ function Setup-HubProject() { $envExample = Join-Path $projPath ".env.example" $envFile = Join-Path $projPath ".env" if ((Test-Path $envExample) -and -not (Test-Path $envFile)) { - Copy-Item $envExample $envFile - Write-Host " 已复制 .env.example -> .env" -ForegroundColor Green + Copy-EnvFileLf $envExample $envFile + Write-Host " 已复制 .env.example -> .env (LF)" -ForegroundColor Green } } Write-Host " 完成: $venvPy" -ForegroundColor Green diff --git a/manual_trading_hub/scripts/fix_env_crlf.sh b/manual_trading_hub/scripts/fix_env_crlf.sh index b0d9e5c..91796f3 100644 --- a/manual_trading_hub/scripts/fix_env_crlf.sh +++ b/manual_trading_hub/scripts/fix_env_crlf.sh @@ -29,4 +29,6 @@ for d in "${dirs[@]}"; do fi done -echo "完成,共修复 ${fixed} 个 .env。请执行: pm2 restart ecosystem.config.cjs" +echo "完成,共修复 ${fixed} 个 .env。" +echo "请重启子代理: cd ${REPO}/manual_trading_hub && pm2 restart manual-agent-gate manual-agent-gate-bot manual-agent-binance manual-agent-okx" +echo "或: bash scripts/pm2_hub.sh restart" diff --git a/manual_trading_hub/scripts/run_agent.sh b/manual_trading_hub/scripts/run_agent.sh index 4a613d7..98c4995 100644 --- a/manual_trading_hub/scripts/run_agent.sh +++ b/manual_trading_hub/scripts/run_agent.sh @@ -20,6 +20,9 @@ if [[ ! -x "${VENV_PY}" ]]; then fi if [[ -f .env ]]; then + if grep -q $'\r' .env 2>/dev/null; then + echo "警告: $(pwd)/.env 含 Windows 换行(CRLF),请在仓库根执行: bash manual_trading_hub/scripts/fix_env_crlf.sh" >&2 + fi if ! load_dotenv_file ".env"; then echo "错误: $(pwd)/.env 加载失败" >&2 exit 1 diff --git a/manual_trading_hub/scripts/start_agents_3screen.sh b/manual_trading_hub/scripts/start_agents_3screen.sh index 2a45f93..dbdd5eb 100644 --- a/manual_trading_hub/scripts/start_agents_3screen.sh +++ b/manual_trading_hub/scripts/start_agents_3screen.sh @@ -53,11 +53,11 @@ start_one() { fi screen -dmS "${name}" bash -c " cd '${work}' || { echo 'cd failed' >>'${logf}'; exit 1; } -set -a +# shellcheck source=lib_load_dotenv.sh +source '${HUB_DIR}/scripts/lib_load_dotenv.sh' if [[ -f .env ]]; then - . ./.env + load_dotenv_file .env >>'${logf}' 2>&1 || echo 'load .env failed' >>'${logf}' fi -set +a export EXCHANGE='${exchange}' PORT='${port}' HOST=127.0.0.1 exec '${VENV_PY}' '${AGENT_PY}' >>'${logf}' 2>&1 " diff --git a/manual_trading_hub/scripts/start_hub_screen.sh b/manual_trading_hub/scripts/start_hub_screen.sh index d505d1f..010f8f2 100644 --- a/manual_trading_hub/scripts/start_hub_screen.sh +++ b/manual_trading_hub/scripts/start_hub_screen.sh @@ -49,11 +49,11 @@ fi screen -dmS mt-hub bash -c " set -e cd '${HUB_DIR}' -set -a +# shellcheck source=lib_load_dotenv.sh +source '${HUB_DIR}/scripts/lib_load_dotenv.sh' if [[ -f .env ]]; then - . ./.env + load_dotenv_file .env fi -set +a export HUB_AGENTS='${HUB_AGENTS}' ${EXTRA_NAMES} export HUB_HOST='${HUB_HOST}'