| |
#!/bin/bash
# 服务名称
serverName="everything2fames"
# 当前目录
DIR=$(pwd)
# 检查进程是否运行并杀死进程
check_process() {
local pid
pid=$(pgrep -f "$DIR/${serverName}")
if [ -n "$pid" ]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Killing ${serverName} with PID $pid..."
kill -9 "$pid"
return 0
else
return 1
fi
}
# 启动进程
start_process() {
echo "Building ${serverName}..."
if ! go build -o "$DIR/${serverName}" "$DIR/main.go"; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Build failed. Exiting..."
exit 1
fi
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting ${serverName}..."
nohup "$DIR/${serverName}" > "$DIR/${serverName}.log" 2>&1 &
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${serverName} started with PID $!"
}
# 第一次启动时检查并杀死进程
if check_process; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${serverName} was running and has been killed."
fi
# 启动进程
start_process
# 主循环
while true; do
current_time=$(date +"%H:%M")
#每晚自动构建与重启服务
if [ "$current_time" = "23:59" ]; then
if check_process; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${serverName} was running and has been killed."
fi
echo "Starting ${serverName}..."
start_process
else
if ! pgrep -f "$DIR/${serverName}" > /dev/null; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${serverName} is not running. Starting..."
start_process
fi
fi
sleep 3 # 每分钟检查一次
done
系列Course并未All上架,处于先行测试阶段