#!/usr/bin/env bash # 如果不是在 bash 下执行,则尝试用 bash 重新执行,避免用 sh 导致语法错误 if [ -z "$BASH_VERSION" ]; then if command -v bash >/dev/null 2>&1; then exec bash "$0" "$@" else echo "This script requires bash. Please run with bash." >&2 exit 1 fi fi # 获取输入参数的时间,默认值为昨天的日期 if [ -n "$1" ]; then data_time=$1 else yesterday=$(date +%s) yesterday=$((yesterday - 86400)) data_time=$(date -d "@$yesterday" +"%Y-%m-%d") fi # 可选:第二个参数为每批大小,第三个参数为批次间隔(秒) BATCH_SIZE=${2:-10} BATCH_INTERVAL=${3:-300} # 日志文件路径(可通过第4个参数覆盖),保持默认兼容性 # LOG_FILE=${4:-./statistics_handle.log} LOG_FILE=${4:-/app/logs/statistics_handle_v2.log} # 确保日志目录存在(先创建目录,避免后面写日志失败) mkdir -p "$(dirname "$LOG_FILE")" # 写入日期到日志(使用 printf 更可靠) printf "\n# DATE: %s - STARTED AT: %s\n" "$data_time" "$(date '+%Y-%m-%d %H:%M:%S')" >> "$LOG_FILE" # 主机地址 # host_url="http://admin.newsdk.com" # host_url="http://devadmin.qmgames.cn" # 测试 host_url="http://admin.qmgames.cn" # 正式 # 定义 URL 列表(每行一个 URL) url_list=" $host_url/retaine_platform/reg_num?day=$data_time $host_url/retaine_platform/role_num?day=$data_time $host_url/retaine_platform/act_num?day=$data_time $host_url/retaine_platform/recharge_num?day=$data_time $host_url/retaine_platform/reg_total?day=$data_time $host_url/retaine_game/reg_num?day=$data_time $host_url/retaine_game/role_num?day=$data_time $host_url/retaine_game/act_num?day=$data_time $host_url/retaine_game/recharge_num?day=$data_time $host_url/retaine_game/one_stay?day=$data_time $host_url/retaine_game/three_stay?day=$data_time $host_url/retaine_game/four_stay?day=$data_time $host_url/retaine_game/five_stay?day=$data_time $host_url/retaine_game/six_stay?day=$data_time $host_url/retaine_game/seven_stay?day=$data_time $host_url/retaine_game/fifteen_stay?day=$data_time $host_url/retaine_game/thirty_stay?day=$data_time $host_url/retaine_game/reg_total?day=$data_time $host_url/retaine_server/reg_num?day=$data_time $host_url/retaine_server/role_num?day=$data_time $host_url/retaine_server/act_num?day=$data_time $host_url/retaine_server/recharge_num?day=$data_time $host_url/retaine_server/one_stay?day=$data_time $host_url/retaine_server/three_stay?day=$data_time $host_url/retaine_server/four_stay?day=$data_time $host_url/retaine_server/five_stay?day=$data_time $host_url/retaine_server/six_stay?day=$data_time $host_url/retaine_server/seven_stay?day=$data_time $host_url/retaine_server/fifteen_stay?day=$data_time $host_url/retaine_server/thirty_stay?day=$data_time $host_url/retaine_server/reg_total?day=$data_time $host_url/retaine_game_channel/reg_num?day=$data_time $host_url/retaine_game_channel/role_num?day=$data_time $host_url/retaine_game_channel/act_num?day=$data_time $host_url/retaine_game_channel/recharge_num?day=$data_time $host_url/retaine_game_channel/one_stay?day=$data_time $host_url/retaine_game_channel/three_stay?day=$data_time $host_url/retaine_game_channel/four_stay?day=$data_time $host_url/retaine_game_channel/five_stay?day=$data_time $host_url/retaine_game_channel/six_stay?day=$data_time $host_url/retaine_game_channel/seven_stay?day=$data_time $host_url/retaine_game_channel/fifteen_stay?day=$data_time $host_url/retaine_game_channel/thirty_stay?day=$data_time $host_url/retaine_game_channel/reg_total?day=$data_time $host_url/retaine_game_channel/new_act_num?day=$data_time $host_url/retaine_game_channel/new_recharge_num?day=$data_time $host_url/retaine_server_channel/reg_num?day=$data_time $host_url/retaine_server_channel/role_num?day=$data_time $host_url/retaine_server_channel/act_num?day=$data_time $host_url/retaine_server_channel/recharge_num?day=$data_time $host_url/retaine_server_channel/one_stay?day=$data_time $host_url/retaine_server_channel/three_stay?day=$data_time $host_url/retaine_server_channel/four_stay?day=$data_time $host_url/retaine_server_channel/five_stay?day=$data_time $host_url/retaine_server_channel/six_stay?day=$data_time $host_url/retaine_server_channel/seven_stay?day=$data_time $host_url/retaine_server_channel/fifteen_stay?day=$data_time $host_url/retaine_server_channel/thirty_stay?day=$data_time $host_url/retaine_server_channel/reg_total?day=$data_time " # 定义模拟 GET 请求的方法 simulate_get_request() { url=$1 start_time=$(date +%s) response=$(curl -s -G --connect-timeout 10 --max-time 600 "$url") ret=$? end_time=$(date +%s) duration=$((end_time - start_time)) res="ok" if [ $ret -ne 0 ] || [ "$response" != "ok" ]; then res="error" fi echo "## URL: $url - RES: $res - TIME: ${duration}s" >> "$LOG_FILE" } # 循环发送请求,按批次执行,每批执行后休眠 BATCH_INTERVAL 秒 count=0 total=0 # 使用 while read 更可靠地按行读取多行字符串(保留空行跳过) while IFS= read -r url; do # 跳过空行 if [ -z "$url" ]; then continue fi simulate_get_request "$url" count=$((count + 1)) total=$((total + 1)) if [ $count -ge $BATCH_SIZE ]; then echo "### BATCH FINISHED at $(date '+%Y-%m-%d %H:%M:%S'), processed $total urls. Sleeping ${BATCH_INTERVAL}s" >> "$LOG_FILE" count=0 sleep "$BATCH_INTERVAL" fi done <> "$LOG_FILE" fi # 添加结束标记 echo "## FINISHED #" >> "$LOG_FILE"