Results 1 to 9 of 9

Thread: find all processes that hold a file open

  1. #1
    Join Date
    Jun 2015
    Beans
    36

    find all processes that hold a file open

    I have opened a file called 'test' with kate. Wrote some text, wrote it to disk and kept kate open. Then i tried to find which process is holding the file open.

    Code:
    #lsof ./test
    Gives no results.

    Code:
    # fuser -vf ./test
    Code:
    # lsof | grep test
    Also no results.

    Code:
    #lsof
    Does give a lot of output

    Also tried to open the file with vim (and hold it open). But still both commands dont give me any output. Also not when i use sudo. Also no results when i specify the
    fullpath to the open file. System: Kubuntu 23.10.
    Last edited by ValentynPrumers; March 15th, 2024 at 11:24 PM.

  2. #2
    Join Date
    Mar 2011
    Location
    U.K.
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: find all processes that hold a file open

    I use an easy method. Install Stacer GUI, view running processes, search "kate".
    But I'm sure you will get cli suggestions.

  3. #3
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: find all processes that hold a file open

    Code:
    sudo lsof | egrep -i test
    is what I'd use. Just tested it. Looks like lsof has gotten really slow. Took about 60 seconds before any useful output even started. I'm guessing there's a timeout required now - probably another snap related issue.

    Editors that are smart don't open files and hold them. They use a temporary file for modifications, then apply those when the "write" or "save" is requested. I don't use kate, so I don't know how it works. I know the vim created a /tmp/.t.swp when I was editing /tmp/t. /tmp/t didn't show up in the output, just the .swp file.

  4. #4
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    7,717

    Re: find all processes that hold a file open

    fuser is nice and quick but only if you know a specific filename. If you can't umount a drive because it's "in use" then "sudo lsof | grep $mount_point" is the only way I know of. I' may be a little slow, (a couple of seconds on my current machine) but it's reliable.

  5. #5
    Join Date
    Jun 2015
    Beans
    36

    Re: find all processes that hold a file open

    Thanks. For some reason # sudo lsof | grep test now does show me vi is using the file.

    But
    Code:
    sudo  fuser -vf test
    Still does not produce any results. It does seem to work for commands. So if i execute

    Code:
     tail -f /var/log/syslog
    and then on another terminal.

    Code:
     fuser -v /usr/bin/tail
    It does output the process that is using the tail command.
    Last edited by ValentynPrumers; March 19th, 2024 at 10:10 PM.

  6. #6
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: find all processes that hold a file open

    Quote Originally Posted by ValentynPrumers View Post
    Thanks. For some reason # sudo lsof | grep test now does show me vi is using the file.
    See/re-read post #3.

  7. #7
    Join Date
    Jun 2015
    Beans
    36

    Re: find all processes that hold a file open

    Thanks. So processes that read files are not displayed with fuser. What is the best way to find all processes that are using a certain file?

  8. #8
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: find all processes that hold a file open

    Quote Originally Posted by ValentynPrumers View Post
    Thanks. So processes that read files are not displayed with fuser. What is the best way to find all processes that are using a certain file?
    lsof is the only way I know. Just be less restrictive about the filename in the egrep. It doesn't hurt to know how the program actually works. Lots of programs will make a copy of the file to be edited, often using a random name, so that edit collisions don't happen. Whoever writes the copy they'd been using last, wins. This is how Unix has worked for 40+ yrs. In a multi-user system, this is a design choice. The alternative is to have file locking and every process has to check for locks, potentially preventing any access to others. I remember that Netware worked that way, so it was possible for someone to lock a file all day long, not allowing anyone else any access at all.

    Concurrency issues are an important design consideration in multi-user systems. There are times when both options fail, unfortunately.

  9. #9
    Join Date
    Aug 2011
    Location
    52.5° N 6.4° E
    Beans
    6,835
    Distro
    Xubuntu 22.04 Jammy Jellyfish

    Re: find all processes that hold a file open

    Quote Originally Posted by TheFu View Post
    Lots of programs will make a copy of the file to be edited, often using a random name, so that edit collisions don't happen.
    Even if the program doesn't use a temporary file, it may not keep the file open. For a simple application, the typical use would be to open the file, read it to RAM, then close it. What's stored in RAM isn't a byte-for-byte copy of the file, but has a more practical structure. When saving the file, the application opens the file, writes the file from RAM (again, not an exact copy of what's in RAM), then closes the file. Unless you're talking about huge files, the file won't be open for more than a second each time.

Bookmarks

Posting Permissions

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