-2

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).

halfer
  • 19,824
  • 17
  • 99
  • 186
shivams
  • 923
  • 12
  • 27
  • Won't work if you `kill -KILL` the watched application.... (Which is a bad thing to do, `kill -TERM` is preferable) – Basile Starynkevitch Sep 13 '13 at 21:56
  • 2
    Something not entirely unlike this is possible from *inside* the main application process, using [signal handlers](http://linux.die.net/man/7/signal). If you were prepared to write a whole lot of horrible low-level C using the debugger API ([`ptrace`](http://linux.die.net/man/2/ptrace)) you could do it from an external watchdog program as well. From an external *bash script*?! Forget about it. – zwol Sep 13 '13 at 21:57
  • 2
    Be more specific about what you are trying to do. If you can, include the actual program in question and give an example of what kind of "attempt to close/terminate" you are trying to intercept. I think this question has been stripped of all useful background information. – Dietrich Epp Sep 13 '13 at 21:58
  • 3
    And you really should explain what is the targetted application, and **why** exactly **you want to do that;**. (Sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)) – Basile Starynkevitch Sep 13 '13 at 21:59
  • You could do something like this with `ptrace()`, which is what debuggers use to monitor and step through the code of a process. When the process receives a signal, the process is stopped and the monitor is notified it; it can then take actions and resume the process. I don't know enough details to write a real answer, I hope this gets you going in the right direction. – Barmar Sep 13 '13 at 22:08
  • Note that this requires writing a program, I'm not aware of a bash interface to ptrace that would give you this level of control. – Barmar Sep 13 '13 at 22:09
  • 1
    even ptrace() won't let you prevent the process to be killed as soon as SIGKILL is received, this is one of the most basic forms of protection an OS has, it is used in case your program is behaving very very badly to try avoid a whole system crash, – Filippo Savi Sep 13 '13 at 22:24
  • Thanks for all your comments. I've made the question specific and clearer now. – shivams Sep 15 '13 at 06:12
  • @shivams The easiest way to accomplish what you want will be to *modify the source code of okular* so that closing the window automatically does a "save as" over the original. I am quite serious. It's probably about five lines of additional code if you do it that way -- the hard part will be finding where to put them. (Qt will generate some kind of application-fieldable event when you close a window. That's what you're looking for.) – zwol Sep 17 '13 at 00:55
  • Hey thanks Zack!! I never thought of touching the source code. Now I'm gonna try that for sure. – shivams Sep 17 '13 at 09:38

1 Answers1

3

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.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190