Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to lsof - to detect locked file

Tags:

lsof

A file is locked with either a fcntl (non-blocking) or some custom way. So I'm using lsof and checking if pid of a process is in there. If lsof returns blank than nothing is using it.

However lsof from my script takes 200ms.

On windows when i try to test for if the file is locked i just open the file and on error its locked, this takes 5ms. Is there any alternative to lsof to do a quick test to see if something is got a hold of a file?

like image 564
Noitidart Avatar asked Sep 22 '14 19:09

Noitidart


1 Answers

The fuser command is a very smart unix utility used to find which process is using a file, a directory or a socket. It also gives information about the user owning the process and the type of access. READ MORE --digitalocean.com

To show processes accessing a particular directory use :

fuser -uvm /somedir

The below output shows that, when ran in verbose mode, the fuse utility gives information about the USER, PID, ACCESS and COMMAND

root@exampleuser-X55CR:~# fuser -v .
                     USER        PID ACCESS COMMAND
/root:               root       3378 ..c.. vim
                     root       3398 ..c.. bash
                     root       3449 ..c.. bash
                     root      19370 ..c.. bash

fuser is useful in identifying process id opening a particular file.

lsof is useful to find out all file(s) opened by particular process.

for more options that go with fuser you can check thier man page man fuser

here is some :

]$ fuser
No process specification given
Usage: fuser [ -a | -s | -c ] [ -n SPACE ] [ -SIGNAL ] [ -kimuv ] NAME...
             [ - ] [ -n SPACE ] [ -SIGNAL ] [ -kimuv ] NAME...
       fuser -l
       fuser -V
Show which processes use the named files, sockets, or filesystems.

    -a        display unused files too
    -c        mounted FS
    -f        silently ignored (for POSIX compatibility)
    -i        ask before killing (ignored without -k)
    -k        kill processes accessing the named file
    -l        list available signal names
    -m        show all processes using the named filesystems
    -n SPACE  search in this name space (file, udp, or tcp)
    -s        silent operation
    -SIGNAL   send this signal instead of SIGKILL
    -u        display user IDs
    -v        verbose output
    -V        display version information
    -4        search IPv4 sockets only
    -6        search IPv6 sockets only
    -         reset options

  udp/tcp names: [local_port][,[rmt_host][,[rmt_port]]]
like image 163
z atef Avatar answered Nov 08 '22 06:11

z atef