xiaowei-system/scripts/cron-retry-wrapper.sh

34 lines
704 B
Bash
Executable File

#!/bin/bash
# cron-retry-wrapper.sh — 通用重试包装脚本
# 用法: cron-retry-wrapper.sh <script_path> [max_retries] [delay_seconds]
SCRIPT="$1"
MAX_RETRIES="${2:-3}"
DELAY="${3:-5}"
if [ ! -f "$SCRIPT" ]; then
echo "错误: 脚本不存在: $SCRIPT"
exit 1
fi
for i in $(seq 1 $MAX_RETRIES); do
echo "尝试 $i/$MAX_RETRIES: $SCRIPT"
output=$(bash "$SCRIPT" 2>&1)
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "$output"
exit 0
fi
echo "失败 (退出码: $exit_code)"
if [ $i -lt $MAX_RETRIES ]; then
echo "等待 ${DELAY}s 后重试..."
sleep $DELAY
fi
done
echo "所有重试失败"
echo "$output"
exit 1