Cronjob script not renaming file

Hello,
I have below script, which connects to ftp server, pull a file and then it renames it after deleting previous day file. This was working before but now it is not able to rename 'Red_bill.txt' to `Red_bill.V01.txt`. If I run manually 'rename Red_bill.txt Red_bill.V01.txt', I am able to rename, but script is not able to do so. Is something wrong or changed on below script ? Can somebody have a look and help ?

tpvwex@vmtpr01:~> cat /home/vendors2/tpvwex_home/pull_file_wrightexpress.sh
#!/bin/sh
HOST='ftp.trigertpv.com'
USER='Red'
FILE='Red_bill.txt'
cat > /tmp/script_file_$$ <<EOF
    get ${FILE}
    rm Red_bill.V01.txt
    rename ${FILE} Red_bill.V01.txt
    exit
EOF
 sftp -b /tmp/script_file_$$ $USER@$HOST
exit_status=$?
rm -f /tmp/script_file_$$
if [ $exit_status != 0 ];then
   echo "SFTP file transfer is failed for file $FILE" |mailx -s "FTPtrigertpv status"  BSS_C8@Red.com
else
echo "SFTP file transfer is successfull for file $FILE" |mailx -s "FTPtrigertpv status" BSS_C8@Red.com
cp $FILE $FILE.`date +%m%d%y`
fi
tpvwex@vmtpr01:~> 

I would think you might need a cd before you call sftp to be sure that cron is running your script in the directory where the file to be transferred is located.

Is it the rename that is failing? Or, could it be that the file is not found to copy or that you don't have permission to remove the remote file that you are replacing?

Assuming that when your script is run by cron , the file you're moving to the remote host is available; does the sftp command:

    get ${FILE} Red_bill.V01.txt

give you the same errors as the commands:

    get ${FILE}
    rm Red_bill.V01.txt
    rename ${FILE} Red_bill.V01.txt

I login to server with tpvwex and did a manual sftp to ftp server. From there, I am able to run 'rename Red_bill.txt Red_bill.V01.txt' (without doing cd).
My main objective here is, to rename Red_bill.txt to Red_bill.V01.txt. Next day, new Red_bill.txt should come at ftp server and cronjob should be able to pull it again. Now it is not renaming it, so cronjob is pulling same file again.
If I replace those 3 lines and add one which you suggested, I am able to fetch file as Red_bill.V01.txt, but at ftp server, it was not renamed. It is still Red_bill.txt

Am I missing something here?

On ftp server :

  • New "Red_bill.txt" should be over-written hopefully if permissions allowed.
  • If not, you should be having the same old file (check with timestamp) on the server

If you saying your cron will always pull the old file, which means you having same file again and ultimately means the upload at the other end didnt work. Could you please check.

Also, you can always debug it with sftp -v[vv]

On ftp server, I am able to rename Red_bill.txt to Red_bill.V01.txt with same user, so permission should not be an issue.
Cronjob is always pulling Red_bill.txt, which it is supposed to. But cronjob is also supposed to rename is to Red_bill.V01 so that there should be no more Red_bill.txt.
Now, since Red_bill.txt is already exists, cronjob is pulling same file next day also. Ideally, if Red_bill.txt is not there (because it was already renamed to Red_bill.V01.txt yesterday), cronjob should not pull any file.

Ok.
I was able to fix this. I put a prefix - like below

get ${FILE}
    -rm Red_bill.V01.txt
    -rename ${FILE} Red_bill.V01.txt