# Keep kernels alive in parallel evaluations of several scripts

I have written a parallel script file that after initializing the kernels it puts the result of some computation in a file. I provide some external parameters to the script. I would like to run the script several times with different parameters, however every time the script runs it inizialises again the kernels, wasting a lot of time.

The script structure is something like

ClearAll["Global*"];
LaunchKernels[4];
param = ToExpression[ \$ScriptCommandLine[[2]] ];

dat=ParallelTable[RandomReal[],{j,param}];
MaxDat=Max[dat];

s=OpenWrite["datafile.txt"];
WriteString[s,param<>"  "<>ToString@MaxDat<>"\n"];
Close[s];


and I run in terminal through, for instance for param=2 and param=3

wolframscript -script prova.wls 2
wolframscript -script prova.wls 3


Every time I called the command the script launches the kernels. It is possible to inizializes the kernels only during the first run and keep them opened? I had a look at similar questions but I did not find the answer.

• Why do you insist on invoking wolframscript many times with command line parameters? You could write a single program that runs once and does everything you need, for all parameters. Running "scripts" like this is not the most effective way of using Mathematica. If each run is so fast that the time to start parallel kernels matters, why are you even using the command line instead of the notebook interface? – Szabolcs Oct 2 '17 at 9:18
• I agree with you, but I need to keep a ssh session alive from remote and I am trying to follow the suggestion in mathematica.stackexchange.com/questions/151981/…. The computation of every script is not slow compared with the initialization time of the kernels (it depends on the value of the parameter...the code I showed you it is just an example), however it is unnecessary, and I am trying to understand whether it is possible to avoid it. – Galuoises Oct 2 '17 at 10:43
• That explains why you don't want to use notebooks, but I still do not see why you want a separate run for each parameter value. – Szabolcs Oct 2 '17 at 10:46
• I need to analyse the data with some parameters before choosing whether or not generate further data...the choice of the parameter I put depends on the partial results (I cannot automate this stage, unfortunately). – Galuoises Oct 2 '17 at 10:51
• I still don't understand why you can't just run everything in Mathematica instead of invoking multiple scripts from the shell. Mathematica runs interactively, so you can use it even for tasks that require human decision from time to time. The screen` utility helps you run it on the server, disconnect, reconnect, and still find it where it was. – Szabolcs Oct 2 '17 at 10:53