0

I have a file that I edit by hand (using emacs in a terminal window). I have a script that then reads the file, checks it for errors, and also modifies it by doing things like sorting lists in alphabetical order. Before running the script, I always want to save the file and exit the editor, since otherwise I can have conflicts between unsaved changes in the editor and changes the script makes.

Is it possible, on linux, for a program to tell whether or not another program has a file open in read-write mode? In this situation, I would like the script to throw an error and exit.

If not, then I can look at more complicated solutions, such as writing a script as a wrapper for emacs that creates a semaphore file when it sees that it's editing this special file. (I normally invoke emacs through an alias anyway.)

[EDIT] I found a solution that works for me, which is to make use of the fact that emacs automatically creates an invisible semaphore file. I would have deleted the question, but the SE interface doesn't seem to let me do that. One can also explicitly use flock from the command line, e.g., flock a.a /bin/emacs -nw a.a, to get an advisory lock.

2
  • In the absence of such semaphores or locks, seems like it's hit or miss. Emacs reads a file into a buffer and closes the file, at least as far as lsof knows. Commented Oct 25, 2022 at 18:32
  • Checking for an explicit flock will fail if you open a file from within emacs instead of explicitly opening it from the command line. My reading also seems to indicate that an implicit semaphore is an optional feature in emacs 26+. This may work in your case but is it guaranteed as a general solution? Commented Oct 25, 2022 at 18:44

1 Answer 1

0

Is it possible, on linux, for a program to tell whether or not another program has a file open in read-write mode? In this situation, I would like the script to throw an error and exit.

You can inspect the file handles a process has. "Having open" however is a bit of a vague term: To display a file and edit it, you can just open the file, read its content, and close the file handle again. So, no, another program cannot reasonably assume anything about what another user-facing program has open, because that's not a fixed concept.

Your use case sounds like you'd either

  • Want your editor to kick off the external program automatically after you saved, and then re-read the file
  • Make sure your editor notices file changes automatically (which emacs does in many cases)
  • Or conversely enable your external program to watch for files being changed

The last is possible – you can use fanotify on modern Linux and inotify on any Linux (since basically forever, Linux 2.6.13). But: Shell scripting alone can't do it. You need to use a language that can get these notifications from the kernel. You could for example use the pyinotify program (I don't like that, it deals poorly with files with spaces in their name), or use pyinotify as library within a python program. For C++, C, and all the systems languages, there's direct fanotify and inotify calls through the C API.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.