The following solution only works for commands that keep running until you close the program/tab. Unfortunately that’s not the case for most GUI programs and
xdg-open
. In the case of Firefox one could get a list of URLs currently opened andgrep
it, but that’s a neither generic nor ideal solution. As for testing whether a program is running, see this question.
Você pode usar lockfile
do procmail
package . Como o seu man
page contém um exemplo que corresponde exatamente ao seu caso de uso, eu vou citar:
Suppose you want to make sure that access to the file "important" is serialised, i.e., no more than one program or shell script should be allowed to access it. For simplicity's sake, let's suppose that it is a shell script. In this case you could solve it like this:
... lockfile important.lock ... access_"important"_to_your_hearts_content ... rm -f important.lock ...
Now if all the scripts that access "important" follow this guideline, you will be assured that at most one script will be executing between the
lockfile
and therm
commands.
Por padrão, todos os outros scripts aguardarão o desbloqueio do arquivo, testando a cada oito segundos por padrão (altere com, por exemplo, lockfile -5 important.lock
por cinco segundos). Se você quiser apenas verificar se está bloqueado, use o comando clássico test
:
if [ -e important.lock ]; then
# stuff to do if locked
else
# stuff to do if not locked
fi