You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为FastQ验证Shell脚本添加至少5次重试机制以应对网络问题

为FastQ验证脚本添加重试机制(解决网络导致的失败问题)

针对你的需求,我们可以通过给每个SRA ID的验证步骤添加循环重试逻辑来实现最多5次的重试,同时增加短暂延迟避免频繁请求被NCBI限制。以下是修改后的完整脚本:

set -x
PS4='[\\d \\t] '

# 定义重试次数和每次重试的延迟时间(秒)
MAX_RETRIES=5
RETRY_DELAY=3

# Check parameter for error
check=0
# Print fastq-dump executable path
echo $(which fastq-dump)

# Loop through all parameters to check validity
for file in "$@";
do
    cp "${file}" .
    # Extract filename for sampleID
    file_basename=$(basename "${file}")
    id=${file_basename%".id"}
    # Start validation
    echo "Checking ${id}..."
    
    # 初始化重试计数器
    retry_count=0
    success=0
    while [ $retry_count -lt $MAX_RETRIES ]; do
        # 清理之前的测试文件,避免残留影响判断
        rm -f "${id}.test.fastq" "${id}.test.log"
        
        # Download start of fastq
        fastq-dump $(get_ngc()) -X 1 -Z --split-spot "${id}" > "${id}.test.fastq" 2> "${id}.test.log"
        # Get number of lines downloaded to validate for error
        numLines=$(cat "${id}.test.fastq" | wc -l)
        if [ $numLines -gt 0 ]; then
            echo "${id} has data... OK (尝试次数: $((retry_count+1)))"
            success=1
            break
        else
            retry_count=$((retry_count+1))
            if [ $retry_count -lt $MAX_RETRIES ]; then
                echo "${id} 验证失败,${RETRY_DELAY}秒后进行第 $((retry_count+1)) 次重试..."
                sleep $RETRY_DELAY
            fi
        fi
    done
    
    # 所有重试都失败的情况
    if [ $success -eq 0 ]; then
        echo "${id} 经过${MAX_RETRIES}次重试后仍无法获取数据... ERROR"
        check=1
    fi
done
# Exit with error if some fastqs not accessible
if [ $check -gt 0 ]; then
    echo "ERROR: One or more samples have inaccessible fastqs.. exiting"
    exit 1
fi

关键修改点说明:

  • 新增了MAX_RETRIES(最大重试次数,设为5)和RETRY_DELAY(每次重试间隔,设为3秒)两个变量,方便后续调整参数。
  • 为每个ID添加了while循环实现重试逻辑,只有在成功获取到数据(numLines>0)或重试次数耗尽时才退出循环。
  • 每次重试前会清理之前的测试文件(.test.fastq和.test.log),避免上一次失败的残留文件影响本次判断。
  • 重试过程中会输出当前尝试次数和重试提示,便于排查问题。

内容的提问来源于stack exchange,提问作者PesKchan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 11:31:04