[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

Exit out of FTP session if target file is locked for writing

I have a script that will regularily FTP to a directory and download the latest file. Sometimes the file will be busy being written and the FTP transfer will fail.

How to detect this event so I can retry the same file? I'll have the desired filename in a variable. I'll be writing a log of all successful transfers.

Current ftp script:

ftp whatever.com <<EOT
cd files
ls
quit
EOT

The above is called by this script:

#/bin/sh
export file_list=`./ftp-whatever-list.sh`
export last_file
for file_entry in $file_list; do
last_file=$file_entry
done
ftp whatever.com <<EOT
cd images
get $last_file
quit
EOT
echo $last_file >> down_files.log


Thanks,

Roger
0
dsitech
Asked:
dsitech
1 Solution
 
mnashadkaCommented:
Roger, I think that the easiest thing for you to do would be to put the ftp into a function (assuming you're using a ksh or bash script), and get the print statements into a variable, which could then be checked for the search string:

#!/bin/sh

#Move the ftp into a function
function ftp_file
{
  ftp whatever.com <<EOT
  cd images
  get $1
  quit
EOT
}

export file_list=`./ftp-whatever-list.sh`
export last_file
for file_entry in $file_list; do
last_file=$file_entry
done

#Read the output of ftp into a variable
message=$( ftp_file $last_file )
#Check $message for whatever error message you are looking for

echo $last_file >> down_files.log
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now