성공할 때까지 scp 명령을 최대 10회 재시도하세요.

성공할 때까지 scp 명령을 최대 10회 재시도하세요.

scp10번 다시 시도하고 실패하면 오류 메시지를 인쇄하고 싶습니다 .

아래는 내 코드입니다.

#!/bin/bash
FILE=$1;
echo $FILE;

HOMEDIR="/home/ibro";
tries=0;
while (system "scp -P 3337 $FILE ibrahimince\@localhost:$HOMEDIR/Printed/")
do
    last if $tries++ > 10;
    sleep 3;
done

if [ $? -eq 0 ]; then
echo "SCP was successful"
else
echo " SCP failed"
fi

불행히도 다음과 같은 오류가 발생합니다.

npm-debug.log
./test.sh: line 8: system: command not found

다음은 @roaima의 제안을 기반으로 한 자세한 출력입니다.

$ shellcheck myscript
 
Line 3:
echo $FILE;
     ^-- SC2086: Double quote to prevent globbing and word splitting.

Did you mean: (apply this, apply all SC2086)
echo "$FILE";
 
Line 9:
    last if $tries++ > 10;
                     ^-- SC2210: This is a file redirection. Was it supposed to be a comparison or fd operation?

$

코드 수정을 도와주실 수 있나요?

답변1

원하는 매개변수를 변경하거나 파일에 대한 새 변수를 생성하십시오.

#!/bin/bash

# Trap interrupts and exit instead of continuing the loop
trap "echo Exited!; exit;" SIGINT SIGTERM

MAX_RETRIES=10
i=0

# Set the initial return value to failure
false

while [ $? -ne 0 -a $i -lt $MAX_RETRIES ]
do
 i=$(($i+1))
 scp -P 3337 my_local_file.txt user@host:/remote_dir/
done

if [ $i -eq $MAX_RETRIES ]
then
  echo "Hit maximum number of retries, ending."
fi

답변2

retry도구가 이를 수행합니다. 필요에 따라 포기하기 전까지 10번 시도했습니다.

기본적으로 우리는 항상 다시 시도합니다.

~$ retry --times 10 -- scp -P 3337 $FILE ibrahimince@localhost:$HOMEDIR/Printed/
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
ssh: connect to host localhost port 3337: Connection refused
scp: Connection closed
retry: scp returned 255, backing off for 10 seconds and trying again...
~$ 

https://github.com/minfrin/retry

최신 Debian, Ubuntu 및 Nix와 함께 즉시 사용할 수 있습니다.

(면책조항, 저는 이 도구의 원저자입니다)

관련 정보