Results 1 to 5 of 5
I have to read files from a linux server. When I copy a file I receivce just a portion of the file I expected if the process generationg ths file ...
- 04-14-2011 #1Just Joined!
- Join Date
- Apr 2011
- Location
- switzerland
- Posts
- 1
How to detect if a file is used in write mode
I have to read files from a linux server. When I copy a file I receivce just a portion of the file I expected if the process generationg ths file is still writing it.
I read the file from a java apllication using SSH/SFTP.
How can I detect if the file is still used by the writing process ?
- 04-14-2011 #2
From remote?
Well, you could use ssh to call lsof and grep for that filename.
But this is a bit crude..
Usually the source application should write to a temp directory.
Once the processing is done, this app should move it to a destination directory.You must always face the curtain with a bow.
- 04-17-2011 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
As Irithori noted, this is difficult to do when the sender is remote. On Linux, a file write is first to cache/buffer memory, and then it is written physically to disc at intervals. As far as the writer is concerned, the write is complete, but if you are reading the file, it may not be available on disc until the cache buffer is flushed. There are ways around this, but I need more information from you about how you are copying/reading the file from the perspective of the remote client.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 04-20-2011 #4
If you also control the processes that are writing to the file, you can do this by using locks.
There are two types of locks: read and write. A file can have many read locks on it simultaneously. However, if it has a write lock, it can only have one, and it cannot have read locks if there is a write lock. This allows readers of a file to be sure that no one is writing to the file while they are reading.
Locks are purely advisory, not mandatory, so they are not a guarantee that nobody is writing to the file. However, if you control both the writers and the readers, you can use locks.DISTRO=Arch
Registered Linux User #388732
- 04-20-2011 #5
If you are looking for which processes is currently using a specific file from shell script -you can use "lsof" comand-
To check for a file "hi.txt" and processes which are currently using that file at remote-machine. you can use something like -
remote-machine#tail -f /root/hi.txtoutput will belocal-machine#ssh root@remote-machine "lsof /root/hi.txt"
HTHCOMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
tail 11381 root 3r REG 8,1 0 394899 /root/hi.txt- Lakshmipathi.G
-------------------
FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
-------------------


Reply With Quote
