14
$\begingroup$

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

$\endgroup$
20
  • 8
    $\begingroup$ 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 ... $\endgroup$
    – Szabolcs
    Commented Feb 16, 2015 at 21:03
  • 4
    $\begingroup$ 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. $\endgroup$
    – Shane
    Commented Feb 16, 2015 at 21:13
  • 1
    $\begingroup$ closely related $\endgroup$
    – Kuba
    Commented Feb 16, 2015 at 21:24
  • 1
    $\begingroup$ Save on every execution (may be slow): NotebookAutoSave. $\endgroup$ Commented Feb 16, 2015 at 22:01
  • 2
    $\begingroup$ Related: (4037), (6435), (8761), (18380), (26740). $\endgroup$
    – Mr.Wizard
    Commented Feb 16, 2015 at 22:29

3 Answers 3

11
$\begingroup$

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.

$\endgroup$
9
$\begingroup$

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!

$\endgroup$
1
  • $\begingroup$ 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. $\endgroup$ Commented Apr 22, 2017 at 18:36
6
$\begingroup$

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

NotebookSave[]
$\endgroup$
2
  • $\begingroup$ Thanks. I suppose that's my best bet. $\endgroup$
    – Shane
    Commented Feb 16, 2015 at 21:27
  • 2
    $\begingroup$ 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[]]; $\endgroup$
    – Shane
    Commented Feb 18, 2015 at 16:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.