I want to write a Bash script which will be "watching" some application. As soon as an attempt is made to close/terminate this application, the script will try to stop that termination operation for a few milliseconds, do some stuff and then terminate the application.
I am using Okular to read PDF files and annotate them. But the annotations are saved only in the Okular's docdata. However, if you save the file by using "Save As", the annotations are saved in the PDF file itself.
What I am trying to do is to intercept user's attempt to close Okular by clicking the "x" button on the window, and "Save as" the okular file, replacing the original, and then close the application.
This is meant to be a dirty hack to save annotations in PDF file, as this functionality is not provided in Linux by any good PDF "reader" (please don't name libreoffice or gimp, they're not readers).
If the application is killed with
-9
, you can do nothing about it, either inside the application or from outside.The best you could do would be to write a script that users would invoke to cause the application to terminate. That script would be responsible for signaling the app that termination was imminent (via some IPC mechanism), wait for an acknowledgement that it's OK to proceed, and then terminate the app.