2

I have the below script to pull the file from another server.

#!/bin/bash

PROC_DATE=`date "+%Y%m%d"`
CURRENT_DATE=`date`
LOG_FILE="/home/tdata/dw/da/common/AWS_{PROC_DATE}.log"
AWS_CRED_FILE_NAME="/home/tdata/.aws/crnals"

cp -p ${AWS_CRED_FILE_NAME} ${AWS_CRED_FILE_NAME}_${PROC_DATE}
if [ $? -ne 0 ] ; then
        echo "Credentials backup Failed\n" > ${LOG_FILE}
        cat ${LOG_FILE}
        exit 1;
else
        cd /home/tdata/.aws/
        sshpass -f .OP004 sftp -o ConnectionAttempts=1 tdata@OP004 << !
          cd /home/tdata/.aws/
            get crnals
        bye
!
        if [ $? -ne 0 ]
        then
                echo "The credentials copy from OP004 to CP104 failed. Please check and re-run manually" > ${LOG_FILE}
                cat ${LOG_FILE}
                exit 1;
        else
                echo "The credentials copy job completed successfully on ${CURRENT_DATE}" > ${LOG_FILE}
                exit 0;
        fi
fi

But it is getting struck for a very long time just like below and not getting completed at all.

$ sh -x AWS_CRED_COPY.sh
++ date +%Y%m%d
+ PROC_DATE=20221004
++ date
+ CURRENT_DATE='Tue Oct  4 02:07:56 CDT 2022'
+ LOG_FILE=/home/tdata/dw/da/common/AWS_20221004.log
+ AWS_CRED_FILE_NAME=/home/tdata/.aws/crnals
+ cp -p /home/tdata/.aws/crnals /home/tdata/.aws/crnals_20221004
+ '[' 0 -ne 0 ']'
+ cd /home/teradata/.aws/
+ sshpass -f .OP004 sftp -o ConnectionAttempts=1 teradata@OP004 

I even tried adding Timeoutparameter for 20 Secs, that is also not working.

But when I run the SFTP block manually on CLI, it is working well and as expected.

tdata@cp114 -bash /opt/tdata/dw/data/common
$ sshpass -f .OP004 sftp -o ConnectionAttempts=1 teradata@OP004 << !
>           cd /home/teradata/.aws/
>             get crnals
>         bye
> !
Connected to ODRP5004.
sftp>           cd /home/teradata/.aws/
sftp>             get crnals
Fetching /home/tdata/.aws/crnals to canals
sftp>         bye

Any idea on how do we resolve this?

2
  • 1
    The question is tagged bash, the shebang is #!/bin/bash, it's not clear what your interactive shell is, but sh -x AWS_CRED_COPY.sh uses sh. I'm not saying this is the culprit; I'm saying it's good not to randomly switch shells when testing. Commented Oct 4, 2022 at 12:52
  • 1
    Understood. As you said that does't seem to be a culprit. But a good practice. Noted. Thank you. Commented Oct 4, 2022 at 13:24

1 Answer 1

0
+50

I successfully run a snippet like yours.

For running the command directly:

  • Removing the password file, lead to an indefinite halt in the terminal.
  • Giving a wrong password to the file, timed out with Permission denied, please try again..

For running the script gave the same results as above.

I guess you are missing the password file.

Trivia: sshpass takes the string until first newline character.

4
  • The same script(without any changes), i tested and it runs successfully for any other file to be fetched from server A to B. But getting hung indefinitely only for this particular file. The permissions on the file even looks good. Commented Oct 7, 2022 at 6:22
  • 1
    I disapprove the down vote on my answer the reason being I have arrived to the same symptom by some means, which I assumed to be the same cause as the one in this question. I would like to be acknowledged of other possible means if any. Commented Oct 7, 2022 at 11:09
  • 1
    @sabarishjackson within script the sshpass is invoked at location /home/tdata/.aws/, whereas it is run under /opt/tdata/dw/data/common manually. However, the password file is not referenced by absolute path. I even tried fetching the file into its same directory, this only empties the text file in my case, non-the-less it can still fetch it. I still suspect you are missing the password credential file. Commented Oct 7, 2022 at 11:18
  • I got it. This is a silly mistake. Thank you so much for pointing it out @cbugk It is all good and running now. Commented Oct 10, 2022 at 5:56

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.