I would like that when I evaluate a notebook it first quits the last local kernel and then starts local kernel again. Is there any command to do this without using the Evaluation menu in Mathematica?
2 Answers
Quit[]
can be used to terminate the kernel, and then anything else evaluated in the notebook will start it again.
If the reason you want to do this is to make sure that what is run in the notebook does not conflict with any previously defined symbols, then you may instead set menu:
Evaluation > Notebook's Default Context > Unique to This Notebook
If you are wishing to Quit[]
the kernel frequently, then you may wish to set up a keyboard shortcut. For that, see this answer:
Customizing Mathematica shortcuts
If you find that Mathematica slows down in the middle of processing a long notebook, and the latter parts of that notebook do not depend on the earlier parts, you may wish insert this at the beginning of your notebook:
Needs["Utilities`CleanSlate`"]
and then insert:
CleanSlate[]
where you want the effect of Quit[]
-
@Mr.Wizard I think when evaluate a long notebook and edit something in it and then evaluate it again, the evaluation seems slower than first time. this is the reseaon for question about quit Local kernel. – Jalil May 27 '11 at 12:26
-
@Jalil, in that case, I recommend the keyboard shortcut. I use it frequently! – Mr.Wizard May 27 '11 at 12:28
-
@Mr.Wizard Your idea with `Quit[]` does not work in an automated way: the user must evaluate next cell by hand after evaluating `Quit[]`. – Alexey Popkov May 27 '11 at 12:29
-
@Alexey yes indeed, which is why I tried to offer alternatives. It seemed negligent to not at least point out its existence however. If you have other ideas, please post them. I believe it is possible to restart a kernel from a second running kernel, but that seems overly complex for the task at hand, and possibly fragile. If you believe I have misunderstood the question, please say so. – Mr.Wizard May 27 '11 at 12:36
-
@Mr.Wizard Interestingly, is it possible to redefine Shift+Enter keyboard combination in a way that allows to check whether the kernel is fresh or not? – Alexey Popkov May 27 '11 at 12:42
-
@Mr.Wizard The basis of the idea is that FrontEnd will not be closed when the kernel is closed. In this way there may be some way to define keyboard combination that will evaluate some code in the FrontEnd independently of the Kernel. This FE-code could also evaluate some code in the kernel to determine whether it is fresh or not. If it is not fresh, the kernel will be restarted. I do not know whether it is possible but it is possible according to principles of MathLink programming. – Alexey Popkov May 27 '11 at 12:54
-
@Alexey I see now. That is an interesting idea. – Mr.Wizard May 27 '11 at 12:55
The only solution I can suggest is to use slave kernel through MathLink
. It allows you to restart the slave kernel whenever you wish and make additional ruled when it will be restarted automatically. Of course, the master kernel will not be restarted and will be used just as an interface for the slave kernel. The general ideas for this you can find in this thread.

- 1
- 1

- 9,355
- 4
- 42
- 93