# How to correctly disconnect from the kernel?

I'm connecting to and disconnecting from the kernel but I'm doing something wrong. I crash the text formatting in the terminal and got some warnings. There are some instructions to connecting to the kernel, but I almost didn't find anything about the disconnecting from the kernel. This question is related to connection to the remote machine and particularly to my this answer.

I'm doing the following (based on this instructions):

1. Open terminal and run math

Mathematica 9.0 for Linux x86 (64-bit)
In[1]:= y=x^2

2
Out[1]= x


OK, it works fine.

2. Open regular Mathematica.

Create a new kernel configuration (Evaluation menu -> Kernel Configuration Options...)

Arguments to MLOpen:

-LinkName 8000@127.0.0.1,8001@127.0.0.1 -LinkMode Listen -LinkProtocol TCPIP -LinkOptions MLDontInteract -LinkHost 127.0.0.1


Shell command to launch kernel: leave empty (we will do it manually)

Set the notebook's kernel to the one you just set up, and evaluate something (2+2).

3. In the terminal evaluate the following:

link=LinkConnect["8000@127.0.0.1,8001@127.0.0.1",LinkProtocol->"TCPIP"]
(* connect to front end link *)

$ParentLink = link; (* set the front end link as parent link *)  4. Now in the notebook you see that 2+2=4 :) Disconnect from the kernel: links = {$ParentLink, MathLink$ServiceLink, MathLink$PreemptiveLink};
$ParentLink = Null; LinkClose /@ links;  5. In the terminal evaluate y=x^2 one more time In[8]:= y=x^2 Out[8]= RawBoxes[SuperscriptBox[x, 2]]  It's not what you expect to see. Moreover, you can see that $FrontEnd isnt empty

In[9]:= $FrontEnd Out[9]= RawBoxes[InterpretationBox[PanelBox[TooltipBox[ C FrontEndObject C, InterpretationBox[StyleBox[FrontEndObject\ [LinkObject["8000@127.0.0.1,8001@127.0.0.1", 58, 1]], ShowStringCharacters -> True, NumberMarks -> True], FrontEndObject[LinkObject["8000@127.0.0.1,8001@127.0.0.1",\ 58, 1]], Editable -> True, AutoDelete -> True]], FrameMargins -> {{4, 5}, {4, 4}}], -FrontEndObject-, Editable -> False]]  6. Evaluate 2+2 in the notebook and link=LinkConnect["8000@127.0.0.1,8001@127.0.0.1",LinkProtocol->"TCPIP"]$ParentLink = link;


in the terminal one more time. The notebook is now reconnected to the kernel, but you can see warnings like

SetDelayed::write: Tag UpdateDynamicObjects in UpdateDynamicObjects[] is Protected. >>
SetDelayed::write: Tag UpdateDynamicObjectsSynchronous in UpdateDynamicObjectsSynchronous[] is Protected. >>


Except the bad formatting in the terminal and these warnings everything works fine.

The first I can temporary fix by

In[16]:= $Post=TextForm; Out[16]//TextForm= Null In[17]:= y=x^2 2 Out[17]//TextForm= x  But it produces annoying //TextForm. The messages come from the reevaluation of $InstallationDirectory/SystemFiles/FrontEnd/TextResources/GetFEKernelInit.tr which contain the following lines

UpdateDynamicObjects[] := (
FrontEndPrivatechanges = InternalGetTrackChanges[];
If[Length[FrontEndPrivatechanges] =!= 0,

MathLinkCallFrontEnd[FrontEndUpdateDynamicObjectsSynchronous[
FrontEndPrivatechanges]]];
)

UpdateDynamicObjectsSynchronous[] :=
MathLinkCallFrontEnd[FrontEndUpdateDynamicObjectsSynchronous[
InternalGetTrackChanges[]]];


Do you have any experience with disconnecting from the kernel?

-
I haven't done this; but out of curiosity: why do you need it? –  Szabolcs Sep 11 '13 at 23:27
@Szabolcs It is related to the connection to the remote machine and my this post. I've also read your question but I want different: just connect, do some preliminary evaluations, run a big task, disconnect and reconnect after several days when calculations end. –  ybeltukov Sep 11 '13 at 23:46
Personally I gave up on that. Now I just use GNU screen. I'm curious to see if you manage to work out a solution to your problem. I see you already found GetFEKernelInit.tr and related files. That was the only thing I could have added. –  Szabolcs Sep 11 '13 at 23:51
@Szabolcs I already use it (now I just ignore warnings). I'm going to put the calculations and go to sleep. Tomorrow I reconnect from work and check the results :) –  ybeltukov Sep 12 '13 at 0:01

The formatting you're seeing on the command-line is caused as a result of settings the FE makes to the "stdout" stream. Internally, the FE calls this function:

FESetResultForm[FEform_] :=
SetOptions["stdout", FormatType -> FEform];


with the argument for form being whatever the default output format should be. This setting doesn't get reset when the kernel disconnects from the FE, but it's easy enough for you to do call this directly:

SetOptions["stdout", FormatType->OutputForm]


I wasn't able to reproduce your second problem, but if the problem is just the warning, then surely you could shut the warning off. You can't bracket the FE's evaluation of the code in FEKernelInit.tr directly using Quiet, but you could issue an Off command in the terminal followed by an On command in the FE.