본문 바로가기

[OS]Operating System/[Linux]

[Linux] retry logic in script

728x90
반응형

linux command로 script를 (특히 jenkins 배치잡으로) 실행할 때 배치잡이 실패하는 경우 횟수 조건을 줘서 retry 하도록 하는 command를 알아본다.

 

#!/bin/bash
slack_webhook_url = <slack_webhook_url>
retry_limit=3
# seconds
timegap=60
<script command>
job_step_result=$?
echo "job_step_result: $job_step_result"
if [[ $job_step_result -ne 0 ]]; then
    for i in $(seq 1 $retry_limit); do
        echo "retry $i time, waiting $timegap sec..."
        # message to slack
        message="retry $i time, waiting $timegap sec... job $JOB_NAME, $JOB_DISPLAY_URL"
        curl -X POST --data-urlencode "payload={\"channel\": \"#noti-channel\", \"username\": \"user_name\", \"text\": \"$message\", \"icon_emoji\": \":large_orange_circle:\"}" $slack_webhook_url
        # wait
        sleep $timegap
        <script command>
        job_step_result=$?
        if [[ $job_step_result -eq 0 ]]; then
            break
        fi
    done
fi
job_result=$job_step_result
echo "job_result code: $job_result"
if [[ $job_result -ne 0 ]]; then
    message="error job $JOB_NAME, $JOB_DISPLAY_URL"
    curl -X POST --data-urlencode "payload={\"channel\": \"#noti-channel\", \"username\": \"user_name\", \"text\": \"$message\", \"icon_emoji\": \":red_circle:\"}" $slack_webhook_url
    echo "error!"
    exit $job_result
fi

 

 


Reference

반응형