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=
tail
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
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
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,
/proc/
(though that would require raised privileges)