#!/bin/bash # 定义一个函数来在每个服务器目录下运行 go run .go run_server() { server_name=$1 echo "Running $server_name" # 如果之前存在相同名字的 screen 会话,先将其终止 existing_session=$(screen -ls | grep -w "$server_name") if [ -n "$existing_session" ]; then echo "Terminating existing screen session for $server_name" screen -S "$server_name" -X quit fi # 导航到相应的目录 cd "$(dirname "$0")"/$server_name # 使用 screen 运行 go run .go screen -dmS $server_name -L go run $server_name.go # 返回到上一级目录 cd - > /dev/null } # 列出所有服务器目录 server_dirs=("backend" "canteen" "data-transfer" "home-user-auth" "inventory" "map-library" "orders" "product" "product-model" "product-template" "shopping-cart-confirmation" "upload" "webset") # 在每个服务器目录下运行相应的 go 程序 for server_dir in "${server_dirs[@]}"; do run_server $server_dir done