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

Discussion in 'Perl Misc' started by Kuhl, Nov 13, 2008.

  1. Kuhl

    Kuhl Guest

    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.
     
    Kuhl, Nov 13, 2008
    #1
    1. Advertising

  2. Kuhl

    toralf Guest

    Re: How to check whether a file is being opened by other programs in Perl script?

    Kuhl wrote:

    > 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

    --
    MfG/Sincerely

    Toralf Förster
    pgp finger print: 7B1A 07F4 EC82 0F90 D4C2 8936 872A E508 7DB6 9DA3
     
    toralf, Nov 13, 2008
    #2
    1. Advertising

  3. Re: How to check whether a file is being opened by other programsin Perl script?

    Kuhl wrote:
    > 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
    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
     
    Josef Moellers, Nov 13, 2008
    #3
  4. Re: How to check whether a file is being opened by other programs in Perl script?

    Kuhl <> wrote:
    >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
     
    Jürgen Exner, Nov 13, 2008
    #4
  5. Kuhl

    Tim Greer Guest

    Re: How to check whether a file is being opened by other programs in Perl script?

    Kuhl wrote:

    > 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".
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Nov 13, 2008
    #5
  6. Kuhl

    WANG Cong Guest

    Re: How to check whether a file is being opened by other programs in Perl script?

    Kuhl wrote:

    > 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";'
     
    WANG Cong, Nov 13, 2008
    #6
  7. Kuhl

    Tim Greer Guest

    Re: How to check whether a file is being opened by other programs in Perl script?

    WANG Cong wrote:

    > Kuhl wrote:
    >
    >> 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";'


    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.
    --
    Tim Greer, CEO/Founder/CTO, BurlyHost.com, Inc.
    Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers
    and Custom Hosting. 24/7 support, 30 day guarantee, secure servers.
    Industry's most experienced staff! -- Web Hosting With Muscle!
     
    Tim Greer, Nov 13, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    687
  2. George2

    check file opened or not?

    George2, Apr 2, 2008, in forum: C Programming
    Replies:
    0
    Views:
    299
    George2
    Apr 2, 2008
  3. JB
    Replies:
    1
    Views:
    568
  4. loial
    Replies:
    4
    Views:
    438
    Nobody
    Sep 23, 2010
  5. Replies:
    1
    Views:
    110
Loading...

Share This Page