Find the answer to your Linux question:
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 ...
  1. #1
    Just 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 ?

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    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.

  3. #3
    Linux Guru Rubberman's Avatar
    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
    Quote Originally Posted by afournier View Post
    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 ?
    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!

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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

  5. #5
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568

    Lightbulb

    Quote Originally Posted by afournier View Post
    How can I detect if the file is still used by the writing process ?
    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.txt
    local-machine#ssh root@remote-machine "lsof /root/hi.txt"
    output will be
    COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
    tail 11381 root 3r REG 8,1 0 394899 /root/hi.txt
    HTH
    - 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
    -------------------

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...