# Unsaved notebook is crashing: is there any way I can save the state of the MathKernel before I kill it?

So I'm in the middle of generating some plots and all of the sudden the frontend goes dead and I haven't saved my work. I'm using Mathematica 8 in MacOS X.

The MathKernel process is still running and I'm hoping to be able to extract the In/Out history from it through Terminal. Is there a way to do this?

-
I have coredumped the MathKernel and Mathematica processes in question and duly forced the crashing instances to close. I've re-done all my work, but if anyone knows an easy way to extract inputs from the dump (which is around 1.6GB of binary content) that'd be a great prevention against future crashes :) –  Saran Apr 8 '12 at 16:48
While not directly a solution to your problem, you can instruct a notebook to automatically save as soon as an output is generated. That way if the notebook crashes, you'll at least not lose anything which was already output at that point (unless the notebook happens to crash during saving; in that case it might have the opposite effect; however it didn't happen to me yet, and you might reduce the risk by making copies (unfortunately Mathematica keeps the file open all the time [at least on Linux], which probably makes identifying the time when a save was completed hard). –  celtschk Apr 14 '12 at 19:47

If there is such a thing, I want to know it for windows and linux based OS'es also.

Battling with the same problem I have found 2 partial solutions:

1. ctrl+s before hitting shift+enter (I think it's command+s for Mac's)
2. if you ignored #1 then you might still be able to screenshot the code and reenter it

I have tried TextRecognize to restore the text from the image, but it's still a bit off.

Edit:

If your UI is not locked, you can

• Quit the kernel, Evaluation -> quit kernel -> local (or add a shortcut for it)
• Interrupt evaluation

• on notebook interfaces: alt + , or Cmd + Option + .
• on text-based interfaces: ctrl + C
-
Unfortunately, the notebook wasn't responding to my scrolling (or any other action for that matter) and the important bits of code were off-screen... –  Saran Apr 8 '12 at 16:42
+1 I like the creativity of TextRecognize! –  tkott Apr 9 '12 at 15:26

How about putting something like this into init.m

RunScheduledTask[If[FileExistsQ[
FileNameJoin[{$UserDocumentsDirectory, "recovermma.please"}]], Save[FileNameJoin[{$UserDocumentsDirectory, "recovermma.txt"}],In]], 300]


It's pretty basic - the scheduled task just checks every five minutes for the existence of the trigger file and if it's there it saves the definition of In to a text file.

To "recover" the session you just create the trigger file in the appropiate folder and wait up to five minutes.

I am unable to crash the Front End at will so I don't know if it works under those circumstances.

-
Is there a particular reason why you only save In, not also Out? BTW, if you are on a Unix-like system (Linux, OS X), you can "crash" the front end from the shell with kill -9 $PID where $PID has to be replaced by the PID of the front end process (ps helps in finding that; if you have only one front end running, killall is an easier alternative). –  celtschk Apr 14 '12 at 22:42
No particular reason, you could save Out as well. It may even be possible to NotebookSave the entire active notebook, but I don't know if that works when the front end has crashed. There might also be a way to directly poll the front end for responsiveness instead of looking for a trigger file, but I'm not sure how to do that. I'm using Windows 7 and killing the front end process takes out the kernel process too. –  Simon Woods Apr 15 '12 at 11:47
You could use this memory leak to crash your front-end, and then let us know if it works. –  rcollyer May 25 '12 at 2:52
@rcollyer, I tried it but both the front end and kernel processes exited. They seem to be inextricably linked in Windows. –  Simon Woods May 25 '12 at 8:36
It was a thought. –  rcollyer May 25 '12 at 11:25