Checking a file is not being written to

Hello All

I am attempting to write a shell script (bourne shell script) which will copy a tar'd and compressed file from a directory to a staging area but will not know whether the file is still open for write since files are being ftp's to my site at random times during the day.

Once I am convinced that the file is stable (not being written to) I will move it to another directory, uncompress it and untar it and then process the data.

What is a good way to determine that the file is not being written to (ftp from other site is complete) before I move it out of the directory in which it was ftp'd into.

thanks
Joe

p.s. please respond by email: EMAIL ADDRESS REMOVED

From the rules you forgot to read:

As for the question itself, that's tricky. Cooperative locking is possible, but both the shell and the FTP program would need to impliment that.

Let the job which ftp's create a trigger file(with some unique pattern) after it is done with the ftp. This would give you an idea of the completion of the file transfer.

This has worked for me in the past.

lsof /path/2/file/getting/modified

An exit code of 0 shows file in use. 1 shows no activity on that file.

[/tmp]$ /usr/sbin/lsof ~/temp/test.tar
COMMAND   PID    USER   FD   TYPE DEVICE      SIZE    NODE NAME
cp      22551 xxxxxxx    4w   REG    3,1 202321920 8798213 /private/ora/temp/test.tar
[/tmp]$ echo $?
0
[/tmp]$ /usr/sbin/lsof ~/temp/test.tar
[/tmp]$ echo $?
1
[/tmp]$ 

vino

Hi vino

please tell what is lsof.when ever i tried to as man lsof it is failing.my actual problm is clearly what kanejm has wrriten.

i tried like files=`lsof ${ALTAS_IN_DIR}/IINV*.* 2>/dev/null`
after that depends on files value means that is zero or one proceeding(move the files to other directory.).

but error is lsof not found.thats i want know about lsof.

Regards,
MallikarjunaRao

An error of 'lsof: not found' means that lsof is not available in your path. Check the value of the $PATH variable in your environment. Search for lsof on your box. Then add the path to lsof in the PATH variable in your environment. Here are the steps:
1.

echo $PATH
whereis lsof

or (this may take quite a bit longer)

find /usr -name lsof -print
PATH=$PATH:/path/to/lsof; export PATH

--EDIT--
mallikarjuna, in your code you are using *.* to pass filenames to lsof. I don't think that lsof can handle more than one filename at a time. Just check that out.
--/EDIT--

What OS are you running ?

Post the results of uname -a

In my machine, lsof is /usr/sbin/lsof.

Follow what blowtorch says and you should find lsof.

most probably should be in /usr/local/bin/ and not in /usr/bin/ (in solaris)

ANSWERS:OS----------SUN
uname -a--------------SunOS iinr5sun 5.9 Generic_112233-05 sun4u sparc SUNW,Sun-Blade-1000

/usr/sbin lsof not exist

Use the command "fuser $filename" to see the processed. lsof is not a default package. In my solaris machine , fuser is in /usr/sbin/.

~Jackal

Can fuser work if the ftp is initiated from another server?

I normally use the find command to get files that are aged (completed writting for some time)

what is lsof give description please

See this.

LSOF (8)

in my login /usr/sbin
lsof not exist
how can i add this
regards

you can try something like this :

if [ -z "`fuser file.tar 2>/dev/null`" ]
then
# tar file is avalaible
else
# tar file in use
fi