File size: 1,191 Bytes
7695663 c03007d 7695663 da24687 7695663 c03007d f4cb592 7695663 4564c64 7695663 cd8aac4 dae6979 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #!/bin/bash
set -e
# 添加日志函数
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1"
}
# 捕获 SIGTERM 信号,优雅关闭所有后台进程
trap "log '收到终止信号,关闭进程...'; kill \$(jobs -p) 2>/dev/null; exit 0" SIGTERM
# 读取服务配置
services=$(jq -c '.[]' services.json)
# 启动服务
echo "$services" | while read -r service; do
name=$(echo $service | jq -r '.name')
command=$(echo $service | jq -r '.command')
port=$(echo $service | jq -r '.port')
working_dir=$(echo $service | jq -r '.working_dir // "/home/pn/app"')
log "Starting $name on port $port in directory $working_dir"
# 设置环境变量
eval $(echo $service | jq -r '.env // {} | to_entries | .[] | "export " + .key + "=\"" + .value + "\""')
# 切换到工作目录并启动服务
if [ ! -d "$working_dir" ]; then
log "ERROR: Directory $working_dir does not exist"
exit 1
fi
(cd $working_dir && PORT=$port eval "$command") &
# 记录进程ID
pids+=($!)
log "$name started with PID ${pids[-1]}"
done
# 等待服务启动
sleep 5
# 启动 Traefik
exec traefik --configFile=$TRAEFIK_CONFIG_FILE |