1

I have an application (not one that I coded but just one I have) that crashes after a certain amount of time for whatever reason. Is there anyway that I could detect that, that exe has crashed and restart it? I've searched high and low and all I found was a 'Not Responding' code to detect it, which didn't work at all. Any ideas? I know I don't really have any examples of what I tried, but to be honest I only found the one thing and it failed so, I'm out of ideas. Thank you!

Frank
  • 75
  • 2
  • 13
  • The [Process](https://msdn.microsoft.com/en-us/library/system.diagnostics.process(v=vs.110).aspx) class should prove useful. – mason Jun 17 '15 at 12:50
  • Does the application return a valid returncode so you can distinguish between a simple ending of the process and a crash? – Ralf Jun 17 '15 at 13:00
  • if you run only one instance of it , you can look if it still running http://stackoverflow.com/questions/262280/how-can-i-know-if-a-process-is-running – LordTitiKaka Jun 17 '15 at 13:00
  • Careful with `whatever reason` and `crashes` Programs crash for different reasons in different ways and the solutions to detect, clean up and recover them can change depending on the circumstances. If your app is `Not respsonding` it means it no longer pumping messages, which you can detect with `SendMessage`, you can then get the applications command line. Kill the process and restart it. – James Jun 17 '15 at 13:01

2 Answers2

5

I have an idea for you. It's not detecting errors but restart your exe when is closed. (To close your exe, close window of script before)

Create a .bat file

@echo off
:run
/path/to/your/exefile.exe
goto run

EDIT

For information : a crash of application does not the same thing of "Not Responding" status. This status means your program is locked in loop for example or during a long procedure etc... A crash, implies that the processus was killed. For me at least :)

I make this script for this specific case.

@echo off

:run

REM kill your exe if not responding
taskkill /f /im "notepad++.exe" /fi "STATUS eq NOT RESPONDING" >nul

tasklist /nh /fi "imagename eq notepad++.exe" /fi "status eq running" | find  /i "notepad++.exe"  >nul && ( 
    echo Notepad is running
    REM program running nothing to do
) || ( 
    echo Notepad was not found ! Restarting notepad...
    REM start for launch .exe without waiting
    start "C:\Program Files (x86)\Notepad++" notepad++.exe
)

REM Wait 1 second
timeout /t 01>nul 

goto run

For test this, I launched my scrpit. Notepad is launched. I ask to notepad to find "something" in C:\ with *.* in files filters.

In task manager I click on "End of task" in "Application" tab. A windows Popup appears with "Program not responding" and "Finish now" option.

And imediately (depends of this line timeout /t 01>nul), the script restart Notepad !

Matthieu
  • 150
  • 7
  • To detect errors, use "errorlevel" i find a post with an exemple [here](http://stackoverflow.com/questions/13150676/using-errorlevel-in-a-batch-file-to-know-if-a-program-exited-normally) – Matthieu Jun 17 '15 at 13:05
  • And when my application crashes, this will restart it? – Frank Jun 17 '15 at 13:11
  • Yes because the script wait during the execution of your exe and when the exe was closed (or crashed) the execution of script continu and execute "goto run" clause. This clause jump to "run:" tag and execute again your exe. I use it to restart automatically a server. – Matthieu Jun 17 '15 at 13:17
  • Doesn't work for me, I do it for the calculator application, and it crashes like so: http://prntscr.com/7j66ql (I made it crash) and it opens with that batch script but nothing happens after it crashes. – Frank Jun 20 '15 at 12:28
  • See "EDIT" in my asnwer for "Not Responding" program – Matthieu Jun 20 '15 at 16:50
  • @Frank Could you edit the title of your question with "Restart a crashed or Not responding exe" if you can ? – Matthieu Jun 20 '15 at 20:51
  • http://prntscr.com/7jxrkj Not responding? I assume? – Frank Jun 22 '15 at 13:33
  • Oh sorry I read too fast. Test my EDIT, it will work too. – Matthieu Jun 22 '15 at 14:40
0

First of all would it not be an idea to find out why your application is crashing? instead of blocking the crash? try to see what the reason of the crash is.

However if you just want to detect and stop the crash you might want to check out these solutions:

Best way to detect an application crash and restart it
How do I detect an application crash

It might be good to read through these, the best solution I found was:

How about creating a wrapper application that launches the faulty app as a child and waits for it? If the exit code of the child indicates an error, then restart it, else exit. (Answer taken from Vinko Vrsalovic)

Community
  • 1
  • 1
RDAxRoadkill
  • 414
  • 5
  • 24