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.
flockwill 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?