32

When I have entered some bad code and hit shift+enter before thinking about the perils of finite RAM my computer obviously starts suffering. I have found two options that usually lets me recover without rebooting or in other ways lose the entire notebook.

The first is to switch to a shell and kill -9 the PID of MathKernel process.
The other is to go to the menu Evaluation->Quit Kernel->Local (note that Alt+. and Alt+, are completely ignored by now), by the time I have reached that menu my computer is on its last breath and seconds are of utmost importance. It usually takes seconds just to render the "Do you really want to quit the kernel?" dialog box. I think I would have better success rate if it just quit the kernel as fast as possible. Can I disable this dialog box?

ssch
  • 16,590
  • 2
  • 53
  • 88

2 Answers2

28

You can add this to your init file, (or just try it out for the current session). It will add a Quit to your key events. This Quit doesn't open a confirm dialog.

FrontEndExecute[
 FrontEnd`AddMenuCommands["MenuListQuitEvaluators",
  {MenuItem["AddMenu &Quit",
    FrontEnd`KernelExecute[ToExpression["Quit[]"]],
    MenuKey["q", Modifiers -> {"Control"}],
    System`MenuEvaluator -> Automatic]}]]

ref. https://mathematica.stackexchange.com/a/6227/363

Alternatively, it can be added to MenuSetup.tr like so:

Menu["Quit Local",
{
  MenuItem["Quit Kernel",
    FrontEnd`KernelExecute[ToExpression["Quit[]"]],
    MenuKey["q", Modifiers -> {"Control"}],
    MenuEvaluator -> Automatic]
}]
Chris Degnen
  • 30,927
  • 2
  • 54
  • 108
  • Dengen: interesting, does this interrupt a running calculation? It looks to me as if the Quit is queued and won't do what the OP desires... – Albert Retey Jan 06 '13 at 20:58
  • @AlbertRetey I didn't feel masochistic enough to actually fill up my RAM so I tried with Pause[100]; and the kernel still quit :) – ssch Jan 06 '13 at 21:06
  • @Albert - I don't know if it's any better than usual kernel quit from the menu. – Chris Degnen Jan 06 '13 at 21:09
  • Thanks! Seems the "AddMenu &Quit" should be just "&Quit" or shows up with "AddMenu" in the menu. One odd thing is that putting it in "$UserBaseDirectory/FrontEnd/init.m" it only works once and is then overwritten (along with all other preferences) but putting it in "$UserBaseDirectory/Kernel/init.m" a new menu item pops up each time a kernel is restarted, a minor matter though :) – ssch Jan 06 '13 at 21:09
  • 1
    @ssch - Probably simpler to put it in your MenuSetup file then. I have just added this to my answer. – Chris Degnen Jan 06 '13 at 21:16
  • 1
    Yea that's better – ssch Jan 06 '13 at 21:29
  • 1
    @Dengen: sorry, seems to work as desired. When I tried it yesterday it didn't, but I can't remember what exactly I tried on which version. Today with Version 9 (64bit) on Windows 7 it seems to interrupt a running calculation... – Albert Retey Jan 07 '13 at 16:10
  • For a better understanding of the second solution, I think it's better to mention this answer :) – xzczd Mar 25 '14 at 07:14
  • That hangs Mathematica 10.0.1. Any idea how to get it working again? – Rolf Mertig Nov 25 '14 at 10:12
  • 1
3

My approach is to use a macro I created in a 3rd-party task automation program (Keyboard Maestro for MacOS; I believe alternatives exist for Windows and Linux). Thus, rather than modifying MMA, I assigned a shortcut to a macro that executes MMA's normal sequence for quitting the kernel.

Specifically, when I press Cmd+Shift+Q, the macro does: Evaluation->Quit Kernel->Local->Quit.

One advantage of this approach is that, so long as Wolfram does not change the menu steps for quitting the kernel, it should work reliably in all versions of MMA.

I'm not familiar with other programs, but creating the macro in Keyboard Maestro is fairly easy; I just had to select three panels from its library of "Actions", add them to the macro, and fill them in appropriately. Here's a screenshot of it:

enter image description here

theorist
  • 3,633
  • 1
  • 15
  • 27