Operating System - HP-UX
1834689 Members
2171 Online
110069 Solutions
New Discussion

Testing filesize in posix

 
SOLVED
Go to solution
Gerald Bush_2
Occasional Advisor

Testing filesize in posix

Hello all,
An interesting problem I have run into. I will give you a general overview so as not to bore anyone.

I receive files via ftp all day long. Upon receipt I must then send them out via dialup. The last step of automating the process is an apparently simple script that simply waits for the file size to stablize and remain constant for a certain period of time to avoid sending out partial files.

I am certain I am not the only person who has run across this problem. Suggestions anyone?

jiin
8 REPLIES 8
Darrell Allen
Honored Contributor

Re: Testing filesize in posix

Hi,

You could use fuser or lsof to see if the file was in use.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
James R. Ferguson
Acclaimed Contributor

Re: Testing filesize in posix

Hi Gerald:

You could use 'wc -c' to assay the size of the file in characters. Capture the value; sleep for a short period; retest; and if the value is the same, consider the process done; otherwise repeat the loop.

Regards!

...JRF...

John Palmer
Honored Contributor

Re: Testing filesize in posix

Gerald,

'fuser' works, you can code it thus:-

if [[ -n $(fuser 2>/dev/null ]];
then
fi

Regards,
John
S.K. Chan
Honored Contributor

Re: Testing filesize in posix

Say if you got these files coming into a "common" location or directory. Aprat from check the file size overtime to make sure they stabalized you can add one more check ie the file count in that directory. If it increases by 1, then you know the next file in being ftp into this directory, and you can proceed with processing the previous file.
harry d brown jr
Honored Contributor
Solution

Re: Testing filesize in posix

Gerald,

For POSIX shell documentation on checking filesizes use:

man sh-posix

and use "lsof filename" to see if anyone has it open!

*************************
If you recieve these files from some automated process have that process modified to do this (I've been doing this for over 10 years now without any issues):

Data file is received, with some conventional naming like .dat, and upon completion of sending the data file, the sending process is to write a blank file with the same PREFIX but with a suffix of .RDY.

Here is an example:

Sender sends the file banktrans_20020501.dat

when it is done, the sender sends a blank file, or even with some useful info in it:

banktrans_20020501.rdy

The receiver ONLY looks for files that match "*\.rdy" to be processed.

Saves everyone a lot of headaches!!!

live free or die
harry
Live Free or Die
John Palmer
Honored Contributor

Re: Testing filesize in posix

Another alternative if you have control over the sending process is to send it as a filename that your receiving process ignores and finally rename it e.g.:

put
rename

Regards,
John
A. Clay Stephenson
Acclaimed Contributor

Re: Testing filesize in posix

Hi:

Rather than looking at filesize a better way is to see if the file has been modified in some period of time. The attached perl script will do the trick:

SECONDS=60
FILENAME=myfile.dat

fileage.pl -m -s ${SECONDS} ${FILENAME}
STAT=$?
if [ ${STAT} -eq 0 ]
then
echo "${FILENAME} has not been modified in"
echo "the last ${SECONDS} seconds."
echo "It is safe to copy."
else
echo "${FILENAME} is not safe to copy."
fi

The script silently report whether or not a file has been modified. fileage.pl -u gives full usage.

If it ain't broke, I can fix that.
Gerald Bush_2
Occasional Advisor

Re: Testing filesize in posix

Thanks everyone for all the help. The solution will be to use the *.rdy empty file method as this avoids any network hiccups. The fuser solution is viable also and the code helped especially. I will use that in the future.

Normally LSOF would have been one of my first thoughts, however 11i doesn't always like to compile the 11.00 depot applications and this is the case with LSOF.

Thanks a million for all the help.

jiin