1832086 Members
2933 Online
110037 Solutions
New Discussion

Scripting help

 
Terrence
Regular Advisor

Scripting help

My weak scripting abilties fail me. I need to know when a file that is being created by an application is finished being written to. My current script finds the file since it exists, but because it's still being written to the file it ftp's to another location is a truncated unfinished version.

My thought was since I find the file by it's name, maybe I could somehow check it's creation date and time to make sure it's at least five minutes old. That way I would know the process is finished writing to it .(since the time and date are constantly updated as the application writes to the file)

The file finishes at various times each day, bit it needs to be ftp'd to it's destination as soon as it's finished.

I have a cron job at seven each morning that starts looking for the file and if it's ready it needs to be ftp'd. So if the script can check it's time and then maybe do an "at" command to check again in five minutes if the file isn't at least five minutes old (and therefore finished).

6 REPLIES 6
Todd McDaniel_1
Honored Contributor

Re: Scripting help

You can use diff...capture the file to a new name then compare

diff orig.file lastest.file

Then continue to compare the file until the output is null then you can ftp it...

Should be a loop to check every N interval maybe a sleep command.
Unix, the other white meat.
curt larson_1
Honored Contributor

Re: Scripting help

one way is to check the file size and then check it again after a bit of time, if the size hasn't changed then you could assume the transfer is complete.

second way is to send a second file after the first one. if the second file is there the first one is finished.
John Poff
Honored Contributor

Re: Scripting help

Hi,

You could use the 'fuser' command to see if a process has the file open, and start the ftp when it doesn't. The only trick is that you have to be root to run 'fuser'.

JP
RAC_1
Honored Contributor

Re: Scripting help

I would check if file is in use or not.

Get lsof tool and check if it is being used by any process? If not ftp it.

You can also use fuser for this.
fuser -u file_name

Man fuser. Get laos tool from HP porting center.

Anil
There is no substitute to HARDWORK
A. Clay Stephenson
Acclaimed Contributor

Re: Scripting help

This is one of those things that Perl's stat() function is just made to handle.

See the attached fileage.pl script and invoke it as fileage.pl -u for full usage.

For your specific question:

TESTFILE=myfile
fileage.pl -s 300 ${TESTFILE}
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "File ${TESTFILE} has not been modified in the last 300 seconds."
else
echo "File ${TESTFILE} has been modified within the last 300 seconds."
fi

If it ain't broke, I can fix that.
John Kittel
Trusted Contributor

Re: Scripting help

This isn't exactly what you asked for, but perhaps you didn't ask for precisely what you need? ... How about making the application that writes the file, since presumably it will know when it is done, do the ftp itself, or activate the other process, script, program or whatever to do the ftp...?