替代lsof - 检测锁定文件

时间:2014-09-22 19:05:55

标签: lsof

使用fcntl(非阻塞)或某种自定义方式锁定文件。所以我正在使用lsof并检查进程的pid是否在那里。如果lsof返回空白而没有使用它。

但是我脚本中的lsof需要200ms。

在Windows上,当我尝试测试文件是否被锁定时,我只是打开文件,如果错误锁定,则需要5ms。是否有任何替代lsof进行快速测试,看看是否有东西被保存?

1 个答案:

答案 0 :(得分:6)

fuser命令是一个非常智能的unix实用程序,用于查找哪个进程正在使用文件,目录或套接字。它还提供有关拥有进程和访问类型的用户的信息。 READ MORE --digitalocean.com

要显示访问特定目录的进程,请使用:

fuser -uvm /somedir

以下输出显示,当以详细模式运行时,fuse实用程序提供有关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在识别打开特定文件的进程ID时非常有用。

lsof可用于查找特定流程打开的所有文件。

有关热熔器的更多选项,您可以查看其手册页man fuser

这里有一些:

]$ 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]]]
相关问题