How to check whether a file is being opened by other programs in Perlscript?


K

Kuhl

Hi, in my Perl script, I need to operate on a file. But I need to
check whether the file is being opened by any other programs, for
example vi, and then my operation will be performed based on this
check result. How to do this check? Thanks.
 
Ad

Advertisements

T

toralf

Kuhl said:
Hi, in my Perl script, I need to operate on a file. But I need to
check whether the file is being opened by any other programs, for
example vi, and then my operation will be performed based on this
check result. How to do this check? Thanks.
perldoc -f flock
 
J

Josef Moellers

Kuhl said:
Hi, in my Perl script, I need to operate on a file. But I need to
check whether the file is being opened by any other programs, for
example vi, and then my operation will be performed based on this
check result. How to do this check? Thanks.

The result of this check is absolutely worthless: before you use the
result of the check, the closed file may be opened, or the open file may
be closed. The file may even cease to exist before you do anything.

Besides: vim (VI iMproved) opens the file, reads it, then closes it
again, then you work on some copy (start editing a file, then, on
another tty look at the editor's open files in /proc/<pidofeditor>/fd).
When you save the result, vi may very create a new file rather than
rewrite the old file (ls -lisa <somefile>; vi <somefile> # and change
it, then save it; ls -lisa <somefile> # the first number is the inode).

It's a similar issue with trying to use the existence of a file as a
locking mechanism: perldoc -q file.lock

Josef
 
J

Jürgen Exner

Kuhl said:
Hi, in my Perl script, I need to operate on a file. But I need to
check whether the file is being opened by any other programs, for
example vi, and then my operation will be performed based on this
check result. How to do this check?

Depending on your OS and the file system there may be no way unless the
programs cooperate, see 'perldoc -q lock':

How can I lock a file?

jue
 
T

Tim Greer

Kuhl said:
Hi, in my Perl script, I need to operate on a file. But I need to
check whether the file is being opened by any other programs, for
example vi, and then my operation will be performed based on this
check result. How to do this check? Thanks.

As others have suggested, look into using file locking. Just keep in
mind, that any program or process that will be reading the file also
needs to obey the same rules with locking. As the saying goes with
file locking "If any single program or process reads, opens or writes
to the file without following the rules, all bets are off".
 
W

WANG Cong

Kuhl said:
Hi, in my Perl script, I need to operate on a file. But I need to
check whether the file is being opened by any other programs, for
example vi, and then my operation will be performed based on this
check result. How to do this check? Thanks.

perl -e 'print qx{/usr/sbin/lsof | grep foo.cfg}, "\n";'
 
Ad

Advertisements

T

Tim Greer

WANG said:
perl -e 'print qx{/usr/sbin/lsof | grep foo.cfg}, "\n";'

Except that won't help if the file is opened/read right after that ran,
before the perl script does its work. That may create a race
condition.
 

Top