Is there a way to check if a file with given name is opened by some process (other than our process)?

I need this on FreeBSD. It would be nice to also support Linux. I write in Perl.

Techniques requiring root access are not suitable.

  • 3
    Could you simply be looking for the fuser command? – Julie Pelletier Dec 20 '16 at 16:24
  • ... or quite possibly lsof. – derobert Dec 20 '16 at 16:46
  • @JuliePelletier Can it be done in Perl, without calling a shell command? (Or is fuser more portable?) – porton Dec 20 '16 at 16:53
  • 1
    Google fuser in perl gives me search.cpan.org/~jstowe/Linux-Fuser-1.5/lib/Linux/Fuser.pm as the first result. – Julie Pelletier Dec 20 '16 at 17:41
  • There isn't, AFAIK, a standard system call to find this info—so you need some native, per-OS code to do it. One of the shell commands is probably the most portable way to get that native code, unless you find something on CPAN. – derobert Dec 20 '16 at 17:56

You should use the fstat command, you can run it as user :

The fstat utility identifies open files. A file is considered open by a process if it was explicitly opened, is the working directory, root directory, jail root directory, active executable text, or kernel trace file for that process. If no options are specified, fstat reports on all open files in the system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.