使用lsof查找单个打开文件的最快方法是什么? [英] What's the fastest way using lsof to find a single open file?

查看:130
本文介绍了使用lsof查找单个打开文件的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试测试单个文件(如果使用lsof打开).有比这更快的方法吗?

I'm trying to test a single file if it is open using lsof. Is there a faster way than this?

$result = exec('lsof | grep filename | wc -l');

if($result > 0) {
    //file is open
}

如果您知道文件名,我认为它们必须是一种仅测试一个文件的方法.我真正需要的是对还是错.

I'm thinking their must be a way to just test for one file if you know the filename. All I really need is a true or false.

推荐答案

我发现了一种更好的方法.使用lsof(在4.63版上进行测试),您可以直接查询特定文件:

I found a much better way. With lsof (tested on version 4.63), you can directly query a specific file:

if lsof filename > /dev/null; then
    # file is open
fi

这篇关于使用lsof查找单个打开文件的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆