Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to detect an application crash and restart it?

What's the best way to detect an application crash in XP (produces the same pair of 'error' windows each time - each with same window title) and then restart it?

I'm especially interested to hear of solutions that use minimal system resources as the system in question is quite old.

I had thought of using a scripting language like AutoIt (http://www.autoitscript.com/autoit3/), and perhaps triggering a 'detector' script every few minutes?

Would this be better done in Python, Perl, PowerShell or something else entirely?

Any ideas, tips, or thoughts much appreciated.

EDIT: It doesn't actually crash (i.e. exit/terminate - thanks @tialaramex). It displays a dialog waiting for user input, followed by another dialog waiting for further user input, then it actually exits. It's these dialogs that I'd like to detect and deal with.

like image 261
Umber Ferrule Avatar asked Sep 16 '08 22:09

Umber Ferrule


1 Answers

Best way is to use a named mutex.

  1. Start your application.
  2. Create a new named mutex and take ownership over it
  3. Start a new process (process not thread) or a new application, what you preffer.
  4. From that process / application try to aquire the mutex. The process will block
  5. When application finish release the mutex (signal it)
  6. The "control" process will only aquire the mutex if either the application finishes or the application crashes.
  7. Test the resulting state after aquiring the mutex. If the application had crashed it will be WAIT_ABANDONED

Explanation: When a thread finishes without releasing the mutex any other process waiting for it can aquire it but it will obtain a WAIT_ABANDONED as return value, meaning the mutex is abandoned and therfore the state of the section it was protected can be unsafe.

This way your second app won't consume any CPU cycles as it will keep waiting for the mutex (and that's enterely handled by the operating system)

like image 141
Jorge Córdoba Avatar answered Oct 04 '22 15:10

Jorge Córdoba