2

I want to create a bash script to determine if my file is already opened by another user.

I have already tried with lsof but it didn't work as i wanted. The files can have different types of extension e.g. txt,pl,conf,cfg etc. Can someone help me?

I have one server with many file .conf. If i write in one file, i can't know if another user write in the same file too. So i'd like to build a script that let me know that.

I tried something like this lsof | grep MyFile or lsof /root/blabla/myfolder/myfile etc. Maybe i don't understand the use of this tool.

i'll try to explain better my problem. There are many users that can be modify .conf file. If two users work on same file, the last saving overwrite the change made from the other user. My script wants to warn the users that the file is already opened by another user and maybe open the file in read-only mode. I already tried with ps aux (thx for the hint) but i can't estimate if my file was recently closed.

7
  • Tried sudo lsof | grep FileName ? Maybe grep -i will do better May 15, 2015 at 21:31
  • 2
    Whats the problem you are having with lsof ?
    – heemayl
    May 15, 2015 at 21:53
  • 2
    "...lsof but it didn't work as i wanted." If you want help, you need to tell us (1) what you want precisely, and (2) how you attempted to use lsof and in what way lsof "didn't work."
    – John1024
    May 15, 2015 at 21:59
  • 1
    And how did you use lsof ? i mean what have you tried ?
    – heemayl
    May 15, 2015 at 22:25
  • your usage pattern seems right to me (apart from possible upper-lowercase issue)....don't you get the desired result?
    – heemayl
    May 15, 2015 at 22:37

2 Answers 2

1

lsof accurately lists all open files.

The "problem" is that most editors open the file, read the contents (into ram), and then close the file.

Editors will then open the file when writing changes.

To see if any editors are using the file, for all users, run

ps aux | grep file name

Example

Open a test.file with nano in one terminal.

In another terminal run the follwoing commands:

bodhi@daemon:~$sudo lsof | grep test.file
[sudo] password for bodhi: 

lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.

Note: no output ;)

Now run ps aux

bodhi@daemon:~$sudo ps aux | grep test.file
bodhi     4736  0.0  0.0 121096  3404 pts/3    S+   17:49   0:00 nano test.file

Now we see the information we want ;)

nano shows up and we are editing test.file

2
  • 1
    It should be mentioned that if another file was opened from inside the editor, then it will not be shown (both through ps aux and lsof). May 16, 2015 at 0:02
  • 1
    probably best to use version control.
    – Panther
    May 16, 2015 at 1:52
0

@bodhi.zazen accurately showed what the reason for your files not being shown in the output of lsof.

Well, since we're lacking a description of your use case, I'll assume that your script

  • does something that takes time
  • changes the content of other files which may have been modified between the time your script starts and the time it's ready to alter the file.

If this is the case, one thing you can do is:

  • get the checksum of the file your script intends to modify early with, for instance: sha1sum <file>
  • do something time consuming
  • check the current checksum of your file. If it's the same as before, it hasn't been modified, if it isn't the same, you can then make a decision and ask the script user to decide whether to overwrite, or to show a diff, etc.

It's not ideal and it's merely a shot in the dark since we're missing context but maybe it will be helpful in some way to you.

2
  • at this point it is probably best to start using version control ;)
    – Panther
    May 16, 2015 at 1:51
  • Hah, did you not see me cringe while I was writing that? :) That's exactly what I thought when I asked for a use-case since jumping through such hoops is probably completely avoidable. May 16, 2015 at 10:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .