Function Repository Resource:

RunOnQuit

Source Notebook

Set an evaluation to run when the Wolfram Language kernel session is terminated

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["RunOnQuit"][eval]

adds eval to a list of evaluations to complete when the current kernel quits.

Details and Options

Each evaluation of ResourceFunction["RunOnQuit"][expr] prepends expr to the list of evaluations to run at kernel exit.
ResourceFunction["RunOnQuit"] does not interfere with existing code defined for $Epilog.

Examples

Basic Examples (2) 

Evaluate an expression when the Wolfram Language kernel session is terminated:

In[1]:=
ResourceFunction["RunOnQuit"][Print["Bye!"]]
Out[1]=
In[2]:=
Quit[]

Add multiple pieces of code:

In[3]:=
ResourceFunction["RunOnQuit"][Print["first"]]
Out[3]=
In[4]:=
ResourceFunction["RunOnQuit"][Print["second"]]
Out[4]=
In[5]:=
Quit[]

Scope (3) 

RunOnQuit[expr] is similar to $Epilog := expr, but the file end.m is still read (if available):

In[6]:=
Definition[$Epilog]
Out[6]=
In[7]:=
ResourceFunction["RunOnQuit"][Print["Quitting!"]]
Out[7]=
In[8]:=
Definition[$Epilog]
Out[8]=

Each time RunOnQuit is used, code is prepended to the sequence of evaluations in $Epilog:

In[9]:=
ResourceFunction["RunOnQuit"][Print["first"]]
Out[9]=
In[10]:=
Definition[$Epilog]
Out[10]=
In[11]:=
ResourceFunction["RunOnQuit"][Print["second"]]
Out[11]=
In[12]:=
Definition[$Epilog]
Out[12]=
In[13]:=
Quit[]

If $Epilog is undefined, RunOnQuit[expr] defines it:

In[14]:=
$Epilog =.
In[15]:=
ResourceFunction["RunOnQuit"][Print["Quitting"]]
Out[15]=
In[16]:=
Definition[$Epilog]
Out[16]=

Evaluations will then be prepended as normal:

In[17]:=
ResourceFunction["RunOnQuit"][Print["Run this first"]]
Out[17]=
In[18]:=
Definition[$Epilog]
Out[18]=
In[19]:=
Quit[]

Applications (4) 

Create a notebook that only persists for the current kernel session:

In[20]:=
With[{nb = CreateWindow[]},
 ResourceFunction["RunOnQuit"][Print["Closing notebook\[Ellipsis]"]; NotebookClose[nb]]
 ]
Out[20]=

Make sure you are logged out of the cloud when the kernel quits:

In[21]:=
ResourceFunction["RunOnQuit"][
 Print["Logging out user: ", $WolframID]; CloudDisconnect[]]
Out[21]=

Clean up any messes that you make:

In[22]:=
myFiles = Table[CreateTemporary[], 5]
Out[22]=
In[23]:=
ResourceFunction["RunOnQuit"][
 Print["Cleaning up ", Length[myFiles], " files\[Ellipsis]"]; DeleteFile /@ myFiles]
Out[23]=

Run all evaluations on quit:

In[24]:=
Quit[]

Possible Issues (2) 

$Epilog will not be evaluated from subkernels:

In[25]:=
ParallelEvaluate[
 ResourceFunction["RunOnQuit"][
  Print["Kernel ", $KernelID, " quitting\[Ellipsis]"]]]
Out[25]=
In[26]:=
CloseKernels[]
Out[26]=

RunOnQuit will fail if $Epilog is Protected:

In[27]:=
$Epilog := Print["Protected!"];
Protect[$Epilog]
Out[23]=
In[28]:=
ResourceFunction["RunOnQuit"][Print["This won't work!"]]
Out[28]=
In[29]:=
Quit[]

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 04 March 2019

Related Resources

License Information