fusenapi/run_all_server.sh

98 lines
2.3 KiB
Bash
Raw Normal View History

2023-07-10 05:05:49 +00:00
#!/bin/bash
2023-09-21 10:33:39 +00:00
single_server_name=$1
2023-09-21 02:55:42 +00:00
go mod tidy
2023-09-21 03:04:04 +00:00
go mod vendor
2023-09-22 03:45:56 +00:00
find /tmp/go-build* -mmin +5 -exec rm -rf {} +
find /tmp/go-link* -mmin +5 -exec rm -rf {} +
run_proxyserver() {
# 定义目录和screen名称
dir_path="./proxyserver"
screen_name="proxyserver"
# 进入目录
cd $dir_path
# 检查是否存在screen session
if screen -list | grep -q "$screen_name"; then
# 结束存在的screen session
screen -S $screen_name -X quit
fi
go build
# 启动新的screen session并运行go程序
echo "run $screen_name"
screen -dmS $screen_name -L ./$screen_name
}
2023-09-21 02:55:42 +00:00
2023-07-10 05:05:49 +00:00
# 定义一个函数来在每个服务器目录下运行 go run <server_name>.go
run_server() {
server_name=$1
2023-09-22 03:27:19 +00:00
# 导航到相应的目录
cd server/$server_name
echo "build $server_name"
2023-09-22 05:37:23 +00:00
go build
2023-07-10 05:05:49 +00:00
# 如果之前存在相同名字的 screen 会话,先将其终止
2023-09-22 03:27:19 +00:00
# 首先尝试关闭已存在的screen会话
2023-07-10 05:05:49 +00:00
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
2023-09-22 03:27:19 +00:00
while [[ $(screen -ls | grep "\.$server_name\s") ]]; do
sleep 0.1s # 等待0.1秒后再次检查
echo "wait for $server_name"
done
2023-07-10 05:05:49 +00:00
fi
2023-09-22 03:27:19 +00:00
# 循环检查screen进程是否存在
2023-09-21 06:53:21 +00:00
[ -f .gitignore ] || (echo "$server_name" > .gitignore && echo "main" >> .gitignore)
2023-07-10 05:05:49 +00:00
# 使用 screen 运行 go run <server_name>.go
2023-09-22 03:27:19 +00:00
echo "Running $server_name"
2023-09-21 03:04:04 +00:00
screen -dmS $server_name -L ./$server_name
2023-07-10 05:05:49 +00:00
# 返回到上一级目录
cd - > /dev/null
}
2023-09-22 03:45:56 +00:00
if [ "$single_server_name" = "proxyserver" ]; then
# 重启proxyserver的逻辑
run_proxyserver
else
server_dirs=() # 初始化一个空数组
if [ -n "$single_server_name" ]; then
server_dirs=("$single_server_name")
else
for dir in server/*/ ; do # 遍历 "server/" 下的所有子目录
dir=${dir%*/} # 删除末尾的 "/"
dir=${dir##*/} # 删除开头的 "server/"
server_dirs+=("$dir") # 添加到数组
done
fi
2023-08-10 11:18:54 +00:00
2023-09-22 03:45:56 +00:00
# 在每个服务器目录下运行相应的 go 程序
for server_dir in "${server_dirs[@]}"; do
run_server $server_dir
done
2023-07-21 03:14:24 +00:00
2023-09-22 03:45:56 +00:00
if [ -n "$single_server_name" ]; then
echo "no proxyserver restart"
else
run_proxyserver
fi
2023-09-21 10:33:39 +00:00
fi
2023-07-10 05:05:49 +00:00
2023-07-19 02:59:13 +00:00
2023-09-21 03:57:31 +00:00
2023-09-21 16:22:53 +00:00
2023-09-22 03:45:56 +00:00
2023-09-21 16:22:53 +00:00