As I'm more familiar with python and Java that with any OS, I know the code I have to write but I don't know how it translates (or indeed IF it translates) to any of the PI OS.

In python on windows it looks like;

    f = open('filename.extension') 
Except IOError:
    x = 1
With f:
 #do a thing

This assumes that the open check looks at only the . extension reader, not the program running it, I have the know how to do that on python but it will be an ugly mess.

Just to specify; I want this to only run the except line if the file is opened in a specified program (maybe via defining the program directory?). I'm working with security, so I can't have a secondary .py or similar. I need the only way to prevent this script from running to be directly deleting the script itself (the methods of which I will most certainly over-secure to the point of impossibility).

Anyone able to provide a few lines that will result in a program specific open check for a specified file that repeats the check indefinitely?

  • Not sure I've got your needs clear but the O/S command lsof will list files that are open. You can then select the file you need and see if the PID matches the program you are concerned about. There are many many options for lsof that will help you narrow down the returned data. Does this sound about the right track?
    – user115418
    Commented May 13, 2020 at 22:16
  • 1
    you may want to look at inotify available for python - that takes care of knowing when a file is accessed in some way (up to you what you monitor, open, write, delete, the list is quite long) - as for knowing which program has opened it, perhaps used in conjunction with the above mentioned lsof, or maybe even scan /proc/ (though that would require raised privileges) Commented May 14, 2020 at 0:38

1 Answer 1


I do not have a clear idea of what you are trying to do, so this is a generic answer. If this doesn't get you where you need to be, perhaps you can clarify your question. Anyway...

It sounds like either fuser or lsof may do what you need. From your description, fuser may work, but "circumstances" could change that. In general fuser is perhaps more widely used than lsof, but lsof may be more versatile. I believe either one could be incorporated in a Python program, but I'll leave that to you. And they could certainly be incorporated in a shell script if you prefer.

For an illustrative example, let's suppose a user has opened the file /home/pi/gammygong.txt via tail; i.e.

$ tail -f /home/pi/gammygong.txt

Note that simply opening a file in your editor (e.g. nano) will not keep the file open as the editor will open it only instantaneously when it is written, then close it (until you write it again). Thus, we'll use tail to keep the file open for our example.

Here's how it would work w/ fuser:

$ fuser /home/pi/gammygong.txt
/home/pi/gammygong.txt: 14985

The output repeats the filename, followed by the process id or pid.

Knowing the process id (pid) allows you to get the name of the command. In this example, we get the name of the command as follows:

$ ps -p 14985 -o comm=

Which would tell us (if we didn't already know) the app/program/command named tail is currently using the pid number 14985.

And so, you could test if this program (tail) is the "specified program" you mentioned in your question.

If it makes any difference, I think (not 100% certain) that fuser is included in the standard set of apps on Raspbian - even the "Lite" version, whereas lsof must be installed. However, there is a Raspbian package available for lsof - which means installation is easy.

Some features of lsof that may make it better-suited to your applications:

lsof has a user-adjustable repeat mode (+|-r [t[m<fmt>]]) that allows it to run repetitively. It also has provisions for adjusting its own security, and avoiding kernel blocks that could stall, delay or inhibit its execution. See man lsof for details.

Here's how it would work w/ lsof:

$ sudo lsof /home/pi/gammygong.txt
tail    14985   pi    3r   REG  179,2       15  794 /home/pi/gammygong.txt

As you see, the default output of lsof is a bit more informative in that it provides the command name and the username, among other things.

Again, there's not enough detail in your question to give a definitive answer, but hopefully this will get you pointed in the right direction. You may edit your question if you'd like to add those details; otherwise, let us know if you have further questions,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.