12

I just Ctrl-Alt-Deleted out of Mathematica after I crashed it while running. I had not saved it all day and had done quite a lot of work on it. When I reopen the file, it shows a version with none of the changes I made today -- I guess I never saved it. I don't think I've actually lost work through not saving in fifteen years. Have I managed to do it today, or is there some way to view scripts that ran in the past but weren't saved?


Moderator's note: I am leaving this question open rather than closing it as a duplicate of one of several linked in the comments because it specifically asks about the possibility of recovering data from a Notebook that was not saved. Please do not post answers explaining how to set up an auto-save system. Such methods should instead be posted in answer to other questions. – Mr.Wizard

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Shane
  • 1,003
  • 1
  • 6
  • 18
  • 8
    I don't think the Mathematica front end has any auto-recovery function. When the front end crashes, any modifications since the last save of the notebook are lost. Unfortunately it happened to me more than once ... – Szabolcs Feb 16 '15 at 21:03
  • I feared this might be the answer. I'm coming over from Matlab where the script saves on every execution. I suppose this will be a painful reminder to save often in the future. Is there any way to have it automatically save on every execution, to avoid future mishaps? – Shane Feb 16 '15 at 21:05
  • Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. – bbgodfrey Feb 16 '15 at 21:06
  • Yeah, no joy there. Re saving, depending on the size of your notebook, saving is not instantaneous, up to agonizingly slow. – Yves Klett Feb 16 '15 at 21:09
  • 4
    On the bright side, WolframAlpha helped my wife and me choose the name of our son. So I guess Lord Wolfram giveth and Lord Wolfram taketh. – Shane Feb 16 '15 at 21:13
  • @Shane o.k. I'll bite. Let's hear it :D – Yves Klett Feb 16 '15 at 21:20
  • 1
  • Haha. Nelson. We wanted a name that was uncommon and on a steady decline. – Shane Feb 16 '15 at 21:25
  • 1
    Save on every execution (may be slow): NotebookAutoSave. – Sjoerd C. de Vries Feb 16 '15 at 22:01
  • If the Undo stack of your Mathematica (v10) stack gets too big it's possible that it stores undo information on disk. This behavior depends on various settings and is largely undocumented and perhaps even not fully implemented yet. You may have such a temporary file somewhere that might be decodable. But I wouldn't give you much chance. – Sjoerd C. de Vries Feb 16 '15 at 22:07
  • 2
  • @Mr.Wizard I've deleted my answer as requested and posted to one of the possible duplicates. 26740 seems the most appropriate but if you feel it is better suited to one of the other questions feel free to move it. – Mike Honeychurch Feb 16 '15 at 23:09
  • @Mr.Wizard I realized that it is a duplicate now after I saw your comments. I don't know if I should delete my answer or not. – Basheer Algohi Feb 17 '15 at 02:03
  • @Algohi You do not need to delete your answer. I was encouraging Mike (and everyone) to "move" answers from duplicates to the original by deleting and reposting, to both give them better exposure for the future and improve site organization. – Mr.Wizard Feb 17 '15 at 02:07
  • 1
    @Mr.Wizard The question is not a duplicate of the questions you have listed. My question was regarding recovering the code following a lockup (whether it was possible), not on avoiding losses through saving. It turns out it is not possible -- though Sjoerd hinted at one long shot. I suppose the best answer to the question then is just "No - it's gone". Granted, I'm sure an answer to that effect exists in one of the questions you cite. That said, none of those will appear pertinent to people like me searching for recovery tips after a crash. Do you disagree? – Shane Feb 17 '15 at 02:34
  • @Shane I agree with you. Confusion arises because the answer you Accepted does not answer the question you asked. A more closely related question is (4037) added to my comment above. If we leave this question Open it would make far more sense not to Accept an answer that has little to do with it, in my opinion. This question needs to be answered with one of: (1) Proof that recovery is possible; (2) An explanation that it is not possible; (3) New functionality that makes it possible. (e.g. (52374)). Do you disagree? – Mr.Wizard Feb 17 '15 at 13:32
  • @Mr.Wizard Haha - I agree. I suppose upon finding out that no pleasing answer is possible (or at least not likely), it's tempting to accept answers that, while not directly answering the question, avoid the question from being an issue in the future. In terms of site organization, though, I agree it's best to avoid that. Go ahead and close the question if you think it best - I trust your judgment as moderator. – Shane Feb 18 '15 at 03:51
  • 1
    @Mr.Wizard A further note - keep in mind (and I'm not saying you're not conscious of this) that it's easy to find information on SE when you already know where it is, or at least have familiarity with the particular community to create good searches. I'm a little more active in Math.SE (though not all that much), and I'm sometimes baffled by how new users miss pre-existing questions/answers. More often than not, though, they have actually searched quite a bit and just couldn't formulate the correct search to reveal what they were looking for. The same happened to me here. – Shane Feb 18 '15 at 03:58
  • @Shane I am well aware that search is difficult; it is why I spend as much of my time as I do hunting for related questions and linking them as I did above. If you un-Accept the answer I shall leave this question open, otherwise I shall close it as it brings nothing unique. I would prefer you to choose the former as some day I hope to see a crash recovery feature. – Mr.Wizard Feb 18 '15 at 16:06
  • @Mr.Wizard Will do. I appreciate your assistance - I learned a fair bit about avoiding code loss looking through the linked questions. – Shane Feb 18 '15 at 16:10

3 Answers3

11

In fact, it is possible to tell Mathematica that "the notebook should automatically be saved after each piece of output generated by evaluation in it"; see documentation. Place

SetOptions[$FrontEndSession, NotebookAutoSave -> True]
NotebookSave[]

at the beginning of a new notebook. When this code is executed, it asks the user where to save the notebook. Once this information is provided, the notebook is saved. More importantly, it is saved each time a cell is executed to produce an Out line.

Thereafter, when the notebook is closed, subsequently reopened, and an Out line produced, the notebook is saved. The downside, of course, is that saving the notebook repeatedly takes time. Moreover, all executed changes, both the good and the bad, are saved.

Note that NotebookAutoSave -> True also can be set using OptionsInspector. With "Show options values" set to "Selected Notebook", type NotebookAutoSave into "Lookup:", and it will appear under "NotebookOptions/File Options

Update: Just saw closely related answer here.

bbgodfrey
  • 61,439
  • 17
  • 89
  • 156
7

If your notebook is hung/non-responsive, and you haven't saved changes, don't give up! You can try to kill the kernel processes without killing Mathematica and revive it.

I just did this on MacOS using the Activity Monitor program. I was evaluating my notebook, and it got stuck and stopped responding. I saw WSMKernelX and WolframKernel processes, selected one, selected the x button to quit, then selected the other one and quit it. I didn't have to do a force quit (for both, at least). The Mathematica process came back alive, and I saved it!

Brad
  • 71
  • 1
  • 1
  • Great advice on killing kernels. I've had to do that many times, and many times dread the time it will take to reload data. But DumpSave helps with that issue. Still... Mathematica crashes A LOT. I know several guaranteed ways to crash the front end and at least 2 ways to get the kernel to die when it shouldn't. – Gregory Klopper Apr 22 '17 at 18:36
6

You can add at the beginning or at the end of your notebook this command:

NotebookSave[]
Basheer Algohi
  • 19,917
  • 1
  • 31
  • 78
  • Thanks. I suppose that's my best bet. – Shane Feb 16 '15 at 21:27
  • 2
    An additional note, if you want to save to a backup file on each execution, this can be done with NotebookSave[], but then it puts you into working in the backup file. If you want to save to a backup file on each execution but stay in your main file, this works for me (though there's likely a more elegant solution out there): Export[NotebookFileName[] <> ".backup.nb", EvaluationNotebook[]]; – Shane Feb 18 '15 at 16:12